Skip to content

Commit 0857256

Browse files
committed
fix(capabilities): unref the wasm transport sleep timer so the host can exit
The wasm exporter races a sleep(timeout) against each HTTP request as a timeout guard (and reuses it for retry backoff). On a fast success the timer is abandoned but, being reffed, kept the Node process alive for up to the request timeout (5 minutes) after the last flush. unref the timer: an in-flight request refs the event loop on its own, so an abandoned/standalone timeout timer must not block process exit. This also fixes the Node 18 CI test jobs: scripts/test.sh now probes for --test-force-exit (Node >= 20.14/22) and runs without it on Node 18, which rejects the flag as unknown. With the unref above, node:test exits cleanly on Node 18 regardless.
1 parent b9303c1 commit 0857256

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

crates/capabilities/src/http_transport.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@ function isDetachedBufferError (err) {
99
}
1010

1111
module.exports.sleep = function (ms) {
12-
return new Promise(resolve => setTimeout(resolve, ms))
12+
return new Promise((resolve) => {
13+
const timer = setTimeout(resolve, ms)
14+
// The exporter races this sleep against each request as a timeout guard (and
15+
// reuses it for retry backoff). An in-flight request refs the event loop on
16+
// its own, so an abandoned timeout timer (e.g. the 5-minute request-timeout
17+
// guard after a fast success) must not keep the host process alive.
18+
timer.unref?.()
19+
})
1320
}
1421

1522
module.exports.setStorage = function (new_storage) {

scripts/test.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@ run_test() {
1414
# exporter's runtime machinery after a flush). For the long-lived real
1515
# consumer that is expected; for the test runner we force a clean exit once
1616
# all tests have finished. Only applies to files that use node:test.
17+
#
18+
# `--test-force-exit` exists on Node >= 20.14/22 but Node 18 rejects it as an
19+
# unknown option. The wasm transport unref's its timeout/backoff timers so the
20+
# process still exits cleanly without the flag; probe for support and degrade
21+
# gracefully on Node 18.
1722
if grep -q "node:test" "$1"; then
18-
node --test-force-exit "$1"
23+
if node --test-force-exit --eval '' >/dev/null 2>&1; then
24+
node --test-force-exit "$1"
25+
else
26+
node "$1"
27+
fi
1928
else
2029
node "$1"
2130
fi

0 commit comments

Comments
 (0)