Skip to content

Commit f1caada

Browse files
habdelraclaude
andcommitted
Harden holdSpecificPort: report bound port, guard close, fix doc
- Report the actually-bound port from server.address() rather than echoing the input, so a stray holdSpecificPort(0) reports the OS-assigned ephemeral port instead of a misleading 0. - Guard the bind-error handler's server.close() against the never-listened case so a synchronous throw there can't leave the outer promise unsettled. - Correct the startIsolatedRealmStack publicPortReservation doc: it's also omitted for a caller-supplied realmServerURL or an explicit compatRealmServerPort, not only a reused context. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 2511652 commit f1caada

2 files changed

Lines changed: 20 additions & 5 deletions

File tree

packages/realm-test-harness/src/isolated-realm-stack.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,9 +623,10 @@ export async function startIsolatedRealmStack({
623623
* upstream by `resolveFactoryRealmServerURL` and threaded down through
624624
* the template build. We adopt it (so the sibling worker-manager /
625625
* realm-server allocations below can't steal the number) and release it
626-
* immediately before the compat proxy binds. When omitted, the public
627-
* port came from a reused context, so we hold the specific number
628-
* ourselves. */
626+
* immediately before the compat proxy binds. Omitted whenever the public
627+
* port wasn't freshly allocated with a holder — a reused context, a
628+
* caller-supplied `realmServerURL`, or an explicit `compatRealmServerPort`
629+
* — in which case we hold the specific number ourselves. */
629630
publicPortReservation?: PortReservation;
630631
}): Promise<RunningFactoryStack> {
631632
if (realms.length === 0) {

packages/realm-test-harness/src/shared.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,14 @@ export async function holdSpecificPort(port: number): Promise<PortReservation> {
499499
return await new Promise<PortReservation>((resolveOuter, rejectOuter) => {
500500
let server = createNetServer((socket) => socket.destroy());
501501
let onError = (error: NodeJS.ErrnoException) => {
502-
server.close();
502+
// The bind failed, so the server never listened — close() hits the
503+
// not-running case. Guard it so a synchronous throw here can't strand
504+
// the outer promise without settling it.
505+
try {
506+
server.close();
507+
} catch {
508+
// never listened — nothing to close
509+
}
503510
if (error.code === 'EADDRINUSE') {
504511
diagnosePortConflict(port)
505512
.catch(() => '')
@@ -519,9 +526,16 @@ export async function holdSpecificPort(port: number): Promise<PortReservation> {
519526
// Swallow late errors after a successful bind so they don't surface
520527
// as unhandled 'error' events.
521528
server.on('error', () => {});
529+
// Report the actually-bound port. For a concrete non-zero port that's
530+
// the requested number; if a caller ever passes 0, listen() picks an
531+
// ephemeral port and this reports the real one instead of a misleading
532+
// 0.
533+
let address = server.address();
534+
let boundPort =
535+
address && typeof address !== 'string' ? address.port : port;
522536
let released = false;
523537
resolveOuter({
524-
port,
538+
port: boundPort,
525539
release: () =>
526540
new Promise<void>((resolveClose, rejectClose) => {
527541
if (released) {

0 commit comments

Comments
 (0)