diff --git a/src/serve/server-threads.ts b/src/serve/server-threads.ts index 273f801..0d2e05f 100644 --- a/src/serve/server-threads.ts +++ b/src/serve/server-threads.ts @@ -1,5 +1,8 @@ import { closeSync, openSync } from 'node:fs'; +import { platform } from 'node:process'; import type { Server } from 'node:net'; + +const fdPath = (fd: number) => (platform === 'linux' ? `/proc/self/fd/${fd}` : `/dev/fd/${fd}`); import { channel } from '../channel.ts'; import { shared } from '../shared/shared.ts'; import { int32atomic } from '../shared/descriptors.ts'; @@ -50,7 +53,7 @@ export function serverThreads( // event loop tracking. This lets socket.destroy() properly close the // original fd (freeing libuv state) while the dup'd fd survives for the // worker to open as a fresh Socket. - const fd = openSync(`/dev/fd/${origFd}`, 'r+'); + const fd = openSync(fdPath(origFd), 'r+'); socket.destroy(); counters.elements[idx].add(1); diff --git a/test/serve/listen.test.ts b/test/serve/listen.test.ts index 2143693..8ae7077 100644 --- a/test/serve/listen.test.ts +++ b/test/serve/listen.test.ts @@ -2,7 +2,10 @@ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { once } from 'node:events'; import { openSync } from 'node:fs'; +import { platform } from 'node:process'; import { createServer as createNetServer, connect } from 'node:net'; + +const fdPath = (fd: number) => (platform === 'linux' ? `/proc/self/fd/${fd}` : `/dev/fd/${fd}`); import { createServer } from 'node:http'; import { int32atomic } from 'moroutine'; import { Int32Atomic } from '../../src/shared/int32-atomic.ts'; @@ -20,7 +23,7 @@ async function acquireLocalFd(): Promise<{ fd: number; close: () => void }> { const client = connect(port); const [peer] = (await once(srv, 'connection')) as [any]; const origFd: number = peer._handle.fd; - const fd = openSync(`/dev/fd/${origFd}`, 'r+'); // dup — new fd, not tracked by libuv + const fd = openSync(fdPath(origFd), 'r+'); // dup — new fd, not tracked by libuv peer.pause(); peer._handle = null; peer.destroy(); diff --git a/test/serve/spike.test.ts b/test/serve/spike.test.ts index aa78dc2..3c424af 100644 --- a/test/serve/spike.test.ts +++ b/test/serve/spike.test.ts @@ -2,7 +2,10 @@ import { describe, it } from 'node:test'; import assert from 'node:assert/strict'; import { once } from 'node:events'; import { openSync } from 'node:fs'; +import { platform } from 'node:process'; import { createServer, connect } from 'node:net'; + +const fdPath = (fd: number) => (platform === 'linux' ? `/proc/self/fd/${fd}` : `/dev/fd/${fd}`); import { Worker } from 'node:worker_threads'; describe('fd-passing spike', () => { @@ -24,7 +27,7 @@ describe('fd-passing spike', () => { const server = createServer(); server.on('connection', (socket) => { // Dup the fd via /dev/fd so the new descriptor is independent of libuv. - const fd = openSync(`/dev/fd/${(socket as any)._handle.fd}`, 'r+'); + const fd = openSync(fdPath((socket as any)._handle.fd), 'r+'); socket.destroy(); worker.postMessage({ fd }); });