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

timers: fix leak when interpreter shutsdown early #422

Merged
merged 1 commit into from
Nov 7, 2023
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ src/js/std.js
src/version.h
node_modules/
docs/api/
test_dir*/
30,021 changes: 15,027 additions & 14,994 deletions src/bundles/c/core/polyfills.c

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ int main(int argc, char **argv) {

int exit_code = TJS_Run(qrt);

if (exit_code == EXIT_SUCCESS) {
// TODO: maybe mark the runtime as aborted and skip some steps?
TJS_FreeRuntime(qrt);
}
TJS_FreeRuntime(qrt);

return exit_code;
}
6 changes: 1 addition & 5 deletions src/js/polyfills/base.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
const core = globalThis.__bootstrap;
import './timers.js';

import queueMicrotask from 'queue-microtask';

globalThis.setTimeout = core.setTimeout;
globalThis.clearTimeout = core.clearTimeout;
globalThis.setInterval = core.setInterval;
globalThis.clearInterval = core.clearInterval;
globalThis.queueMicrotask = queueMicrotask;

Object.defineProperty(globalThis, 'global', {
Expand Down
29 changes: 29 additions & 0 deletions src/js/polyfills/timers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const core = globalThis.__bootstrap;

const timers = new Set();

globalThis.setTimeout = (fn, ms) => {
const t = core.setTimeout(fn, ms);

timers.add(t);

return t;
};

globalThis.clearTimeout = t => {
core.clearTimeout(t);
timers.delete(t);
};

globalThis.setInterval = (fn, ms) => {
const t = core.setInterval(fn, ms);

timers.add(t);

return t;
};

globalThis.clearInterval = t => {
core.clearInterval(t);
timers.delete(t);
};
6 changes: 0 additions & 6 deletions src/timers.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ typedef struct {
JSContext *ctx;
uv_timer_t handle;
int interval;
JSValue obj;
JSValue func;
int argc;
JSValue argv[];
Expand All @@ -47,10 +46,6 @@ static void clear_timer(TJSTimer *th) {
th->argv[i] = JS_UNDEFINED;
}
th->argc = 0;

JSValue obj = th->obj;
th->obj = JS_UNDEFINED;
JS_FreeValue(ctx, obj);
}

static void call_timer(TJSTimer *th) {
Expand Down Expand Up @@ -145,7 +140,6 @@ static JSValue tjs_setTimeout(JSContext *ctx, JSValueConst this_val, int argc, J
CHECK_EQ(uv_timer_init(tjs_get_loop(ctx), &th->handle), 0);
th->handle.data = th;
th->interval = magic;
th->obj = JS_DupValue(ctx, obj);
th->func = JS_DupValue(ctx, func);
th->argc = nargs;
for (int i = 0; i < nargs; i++)
Expand Down
13 changes: 13 additions & 0 deletions tests/helpers/timers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
async function foo() {
throw new Error('oops!');
}


setTimeout(async () => {
await foo();
}, 100);


setTimeout(() => {
console.log('boooo!');
}, 99999);
16 changes: 16 additions & 0 deletions tests/test-timer-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import assert from 'tjs:assert';
import path from 'tjs:path';


const args = [
tjs.exepath,
'run',
path.join(import.meta.dirname, 'helpers', 'timers.js')
];
const proc = tjs.spawn(args, { stdout: 'ignore', stderr: 'pipe' });
const buf = new Uint8Array(4096);
const nread = await proc.stderr.read(buf);
const stderrStr = new TextDecoder().decode(buf.subarray(0, nread));
const status = await proc.wait();
assert.ok(stderrStr.match(/Error: oops!/) !== null, 'dumps to stderr');
assert.ok(status.exit_status !== 0 && status.term_signal === null, 'succeeded');