Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash and memory leak in watch calls #84

Merged
merged 1 commit into from
Feb 29, 2024
Merged
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
47 changes: 29 additions & 18 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
): Promise<WatchResult>;
}

const joinURL = (left: string, right: string) => (left + "/" + right).replace(/([^:])(\/\/)/g, "$1/");

Check warning on line 65 in src/client.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Missing return type on function

Check warning on line 65 in src/client.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Missing return type on function

Check warning on line 65 in src/client.ts

View workflow job for this annotation

GitHub Actions / test (18.x)

Missing return type on function

Check warning on line 65 in src/client.ts

View workflow job for this annotation

GitHub Actions / test (20.x)

Missing return type on function

export class KubernetesRESTClient implements IKubernetesRESTClient {
public constructor(private readonly config: IKubernetesClientConfig) {}
Expand Down Expand Up @@ -197,7 +197,7 @@
};
const request = client.request(requestHeaders);

let body = "";
let lastResponse = "";
let buffer = "";

request.on("error", (err: any) => {
Expand Down Expand Up @@ -227,7 +227,7 @@
}

try {
const parsedBody = JSON.parse(body);
const parsedBody = JSON.parse(lastResponse);

if (isStatus(parsedBody) && parsedBody.status === "Failure") {
debug("watch: failed with status %O", parsedBody);
Expand All @@ -249,28 +249,39 @@
debug("WATCH request on %o received %d bytes of data", absoluteURL, chunk.length);

buffer += chunk;
body += chunk;

let parts = buffer.split('\n');

// the last part is either empty or a part of the following incomplete line
buffer = parts.pop() ?? "";

if (buffer !== "") {
lastResponse = buffer
} else if (parts.length > 0) {
lastResponse = parts[parts.length - 1]
}

// Line is not yet complete; wait for next chunk.
if (!buffer.endsWith("\n")) {
if (parts.length === 0) {
return;
}

try {
const obj: WatchEvent<R> = JSON.parse(buffer);
buffer = "";

const resourceVersion = obj.object.metadata.resourceVersion
? parseInt(obj.object.metadata.resourceVersion, 10)
: -1;
if (resourceVersion > lastVersion) {
debug(`watch: emitting ${obj.type} event for ${obj.object.metadata.name}`);

lastVersion = resourceVersion;
onUpdate(obj).catch(onError);
for (const part of parts) {
try {
const obj: WatchEvent<R> = JSON.parse(part);

const resourceVersion = obj.object.metadata.resourceVersion
? parseInt(obj.object.metadata.resourceVersion, 10)
: -1;
if (resourceVersion > lastVersion) {
debug(`watch: emitting ${obj.type} event for ${obj.object.metadata.name}`);

lastVersion = resourceVersion;
onUpdate(obj).catch(onError);
}
} catch (err) {
onError(err);
}
} catch (err) {
onError(err);
}
});
});
Expand Down
Loading