Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion src/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ import type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
import type { RequestURL, RequestOptions, HttpMethod, RequestMeta } from './Request.js';
import type { RawResponseWithMeta, HttpClientResponse, SocketInfo } from './Response.js';
import symbols from './symbols.js';
import { parseJSON, digestAuthHeader, globalId, performanceTime, isReadable, updateSocketInfo } from './utils.js';
import {
parseJSON,
digestAuthHeader,
globalId,
performanceTime,
isReadable,
updateSocketInfo,
wakeupEventLoopOnDispatch,
} from './utils.js';

type Exists<T> = T extends undefined ? never : T;
type UndiciRequestOption = Exists<Parameters<typeof undiciRequest>[1]>;
Expand Down Expand Up @@ -650,6 +658,8 @@ export class HttpClient extends EventEmitter {
this.emit('request', reqMeta);
}

// wake the event loop so undici's idle socket validation immediate runs, see utils.ts
wakeupEventLoopOnDispatch();
let response = await undiciRequest(requestUrl, requestOptions as UndiciRequestOption);
if (
response.statusCode === 401 &&
Expand Down Expand Up @@ -677,6 +687,7 @@ export class HttpClient extends EventEmitter {
}
// Ensure the previous response is consumed as we re-use the same variable
await response.body.arrayBuffer();
wakeupEventLoopOnDispatch();
response = await undiciRequest(requestUrl, requestOptions as UndiciRequestOption);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type { IncomingHttpHeaders } from './IncomingHttpHeaders.js';
import type { FetchMeta, HttpMethod, RequestMeta } from './Request.js';
import type { RawResponseWithMeta, SocketInfo } from './Response.js';
import symbols from './symbols.js';
import { convertHeader, globalId, performanceTime, updateSocketInfo } from './utils.js';
import { convertHeader, globalId, performanceTime, updateSocketInfo, wakeupEventLoopOnDispatch } from './utils.js';

const debug = debuglog('urllib/fetch');

Expand Down Expand Up @@ -197,6 +197,8 @@ export class FetchFactory {
} as any as RawResponseWithMeta;
try {
await this.#opaqueLocalStorage.run(internalOpaque, async () => {
// wake the event loop so undici's idle socket validation immediate runs, see utils.ts
wakeupEventLoopOnDispatch();
res = await UndiciFetch(request);
});
} catch (e: any) {
Expand Down
13 changes: 13 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ export function performanceTime(startTime: number, now?: number): number {
return Math.floor(((now ?? performance.now()) - startTime) * 1000) / 1000;
}

const noop = () => {};

// undici@8.8.0 defers writing a request to an idle keep-alive socket behind an
// unref'ed setImmediate ("idle socket validation", nodejs/undici#5600). An
// unref'ed immediate cannot wake an idle event loop, so when nothing else wakes
// it the deferred write never happens and the request hangs. Scheduling one
// ref'ed no-op immediate at dispatch time forces the next check phase to run,
// which also runs undici's validation immediate.
// Remove once undici ships nodejs/undici#5606 and the dependency is bumped past it.
export function wakeupEventLoopOnDispatch(): void {
setImmediate(noop);
}

export function isReadable(stream: any): boolean {
if (typeof Readable.isReadable === 'function') return Readable.isReadable(stream);
// patch from node
Expand Down
Loading