-
Notifications
You must be signed in to change notification settings - Fork 18k
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
context: canceling a context should clear the timeout in nodejs #72797
Comments
@golang/wasm |
Looks like maybe we're not cleaning up timers when the program exits? These are created by https://cs.opensource.google/go/go/+/master:src/runtime/lock_js.go;l=267;bpv=1;bpt=0?q=scheduleTimeoutEvent&ss=go%2Fgo. |
Adding something like this in the "runtime.scheduleTimeoutEvent": (sp) => {
sp >>>= 0;
const id = this._nextCallbackTimeoutID;
this._nextCallbackTimeoutID++;
this._scheduledTimeouts.set(id, setTimeout(
() => {
if (this.exited) { // <== this
return
}
this._resume();
while (this._scheduledTimeouts.has(id)) {
// for some reason Go failed to register the timeout event, log and try>
// (temporary workaround for https://github.com/golang/go/issues/28975)
console.warn("scheduleTimeoutEvent: missed timeout event");
this._resume();
}
},
getInt64(sp + 8),
));
this.mem.setInt32(sp + 16, id, true);
}, |
maybe we can cancel all |
diff --git a/lib/wasm/wasm_exec.js b/lib/wasm/wasm_exec.js
--- a/lib/wasm/wasm_exec.js (revision 6fb7bdc96d0398fab313586fba6fdc89cc14c679)
+++ b/lib/wasm/wasm_exec.js (date 1739646571863)
@@ -243,6 +243,10 @@
delete this._goRefCounts;
delete this._ids;
delete this._idPool;
+ for (const id of this._scheduledTimeouts) {
+ clearTimeout(id[1]);
+ }
+ delete this._scheduledTimeouts;
this.exit(code);
},
|
I confirm that this works. However,
|
|
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes (1.24.0)
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Example code: https://go.dev/play/p/j5I16KsARN_i
The HTTP request can be replaced with a query to an SQL database or just any operation that takes
context.Context
. Set the timeout long enough so the operation can finish before the context timeouts. Notice thedefer cancel()
statement.I compiled the program with
GOOS=js GOARCH=wasm go build -o main.wasm
and run the wasm binary on Node.js v22.13.0 with the provided wasm_exec.js. I added several print statements around these lines to help with debugging https://pastebin.com/ZTUnDYwnHere's the Node.js script to run the wasm binary https://pastebin.com/CDNMS5Au
What did you expect to see?
I suppose the connection reset error is expected due to this https://cs.opensource.google/go/go/+/master:src/net/http/roundtrip_js.go;l=49-57?q=jsFetchDisabled&ss=go%2Fgo
What did you see instead?
I suppose it makes sense that calling the
cancel()
function (throughdefer
in this case) should also clear the timeout in the host Node.js runtime.The text was updated successfully, but these errors were encountered: