Skip to content

Commit e6922f8

Browse files
authored
Merge pull request #2665 from sachiniyer/siyer/fix-timeout-signal-watch
fix: timeoutSignal is lost after being overriden with controller.signal
2 parents a0fd40c + b3d7800 commit e6922f8

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/watch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { KubeConfig } from './config.js';
66
export class Watch {
77
public static SERVER_SIDE_CLOSE: object = { error: 'Connection closed on server' };
88
public config: KubeConfig;
9+
private requestTimeoutMs: number = 30000;
910

1011
public constructor(config: KubeConfig) {
1112
this.config = config;
@@ -39,9 +40,8 @@ export class Watch {
3940
const requestInit = await this.config.applyToFetchOptions({});
4041

4142
const controller = new AbortController();
42-
const timeoutSignal = AbortSignal.timeout(30000);
43+
const timeoutSignal = AbortSignal.timeout(this.requestTimeoutMs);
4344
requestInit.signal = AbortSignal.any([controller.signal, timeoutSignal]);
44-
requestInit.signal = controller.signal as AbortSignal;
4545
requestInit.method = 'GET';
4646

4747
let doneCalled: boolean = false;

src/watch_test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,39 @@ describe('Watch', () => {
452452
s.done();
453453
});
454454

455+
it('should timeout when server takes too long to respond', async (t) => {
456+
const kc = await setupMockSystem(t, (_req: any, _res: any) => {
457+
// Don't respond - simulate a hanging server
458+
});
459+
const watch = new Watch(kc);
460+
461+
// NOTE: Hack around the type system to make the timeout shorter
462+
(watch as any).requestTimeoutMs = 10;
463+
464+
let doneErr: any;
465+
466+
let doneResolve: () => void;
467+
const donePromise = new Promise<void>((resolve) => {
468+
doneResolve = resolve;
469+
});
470+
471+
await watch.watch(
472+
'/some/path/to/object',
473+
{},
474+
() => {
475+
throw new Error('Unexpected data received - timeout should have occurred before any data');
476+
},
477+
(err: any) => {
478+
doneErr = err;
479+
doneResolve();
480+
},
481+
);
482+
483+
await donePromise;
484+
485+
strictEqual(doneErr.name, 'AbortError');
486+
});
487+
455488
it('should throw on empty config', async () => {
456489
const kc = new KubeConfig();
457490
const watch = new Watch(kc);

0 commit comments

Comments
 (0)