Skip to content

Commit 0d700c0

Browse files
authored
Merge pull request #884 from dominykas/master
Ignore post-abort request errors
2 parents 25be672 + b088491 commit 0d700c0

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

src/cache.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ export class ListWatch<T extends KubernetesObject> implements ObjectCache<T>, In
131131

132132
private _stop(): void {
133133
if (this.request) {
134+
this.request.removeAllListeners('error');
135+
this.request.on('error', () => {
136+
// void - errors emitted post-abort are not relevant for us
137+
});
134138
this.request.abort();
135139
this.request = undefined;
136140
}

src/cache_test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,53 @@ describe('ListWatchCache', () => {
14451445
const reqOpts: request.OptionsWithUri = opts as request.OptionsWithUri;
14461446
expect(reqOpts.qs.labelSelector).to.equal(APP_LABEL_SELECTOR);
14471447
});
1448+
1449+
it('should ignore request errors after it is aborted', async () => {
1450+
const fakeWatch = mock.mock(Watch);
1451+
const list: V1Pod[] = [];
1452+
const listObj = {
1453+
metadata: {
1454+
resourceVersion: '12345',
1455+
} as V1ListMeta,
1456+
items: list,
1457+
} as V1NamespaceList;
1458+
1459+
const listFn: ListPromise<V1Namespace> = function(): Promise<{
1460+
response: http.IncomingMessage;
1461+
body: V1NamespaceList;
1462+
}> {
1463+
return new Promise<{ response: http.IncomingMessage; body: V1NamespaceList }>((resolve) => {
1464+
resolve({ response: {} as http.IncomingMessage, body: listObj });
1465+
});
1466+
};
1467+
let request = new FakeRequest();
1468+
1469+
let promise = new Promise((resolve) => {
1470+
mock.when(
1471+
fakeWatch.watch(mock.anything(), mock.anything(), mock.anything(), mock.anything()),
1472+
).thenCall(() => {
1473+
request.on('error', (err) => {
1474+
throw new Error(
1475+
`This handler should have been removed and "${err}" should have been silenced.`,
1476+
);
1477+
});
1478+
1479+
resolve(request);
1480+
1481+
return request;
1482+
});
1483+
});
1484+
1485+
const informer = new ListWatch('/some/path', mock.instance(fakeWatch), listFn, false);
1486+
1487+
await informer.start();
1488+
1489+
await promise;
1490+
1491+
await informer.stop();
1492+
1493+
request.emit('error', new Error('Error emitted after abort() was called'));
1494+
});
14481495
});
14491496

14501497
describe('delete items', () => {

src/watch.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface WatchUpdate {
1313
export interface RequestResult {
1414
pipe(stream: Duplex): void;
1515
on(ev: string, cb: (arg: any) => void): void;
16+
removeAllListeners(ev: string): void;
1617
abort(): void;
1718
}
1819

0 commit comments

Comments
 (0)