Skip to content

Commit

Permalink
use emulator.destroy() over emulator.stop() to properly remove v86 in…
Browse files Browse the repository at this point in the history
…stances

emulator.stop sometimes works (when the GC can figure out that .start()
is unreachable and nothing can call into the instance). However, some
resources, such as any WebSocket connection, need to be closed manually.
  • Loading branch information
copy committed Dec 20, 2024
1 parent 5d867bd commit 23411f3
Show file tree
Hide file tree
Showing 20 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/nodejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ process.stdin.on("data", function(c)
if(c === "\u0003")
{
// ctrl c
emulator.stop();
emulator.destroy();
process.stdin.pause();
}
else
Expand Down
2 changes: 1 addition & 1 deletion examples/nodejs_state.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ process.stdin.on("data", async function(c)
if(c === "\u0003")
{
// ctrl c
emulator.stop();
emulator.destroy();
process.stdin.pause();
}
else if(c === "\x1b\x4f\x51")
Expand Down
2 changes: 1 addition & 1 deletion src/browser/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1997,7 +1997,7 @@

$("exit").onclick = function()
{
emulator.stop();
emulator.destroy();
location.href = location.pathname;
};

Expand Down
4 changes: 2 additions & 2 deletions tests/api/clean-shutdown.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
"use strict";

// This test checks that calling emulator.stop() will remove all event
// This test checks that calling emulator.destroy() will remove all event
// listeners, so that the nodejs process cleanly and automatically exits.

const TEST_RELEASE_BUILD = +process.env.TEST_RELEASE_BUILD;
Expand All @@ -28,6 +28,6 @@ const emulator = new V86(config);
setTimeout(function()
{
console.error("Calling stop()");
emulator.stop();
emulator.destroy();
console.error("Called stop()");
}, 3000);
2 changes: 1 addition & 1 deletion tests/api/floppy-insert-eject.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ setTimeout(async () =>
emulator.keyboard_send_text("dir A:\n");
await emulator.wait_until_vga_screen_contains("FDOS <DIR>");
console.log("Got FDOS");
emulator.stop();
emulator.destroy();
clearTimeout(timeout);
//clearInterval(interval);
}, 1000);
2 changes: 1 addition & 1 deletion tests/api/reboot.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ emulator.add_listener("serial0-output-byte", function(byte)
if(serial_text.endsWith("Files send via emulator appear in /mnt/"))
{
console.log("Ok");
emulator.stop();
emulator.destroy();
clearTimeout(timeout);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/api/reset.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ emulator.add_listener("serial0-output-byte", function(byte)
serial_text = "";
if(did_restart) {
console.log("Ok");
emulator.stop();
emulator.destroy();
}
else {
console.log("Calling restart()");
Expand Down
2 changes: 1 addition & 1 deletion tests/api/serial.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ emulator.add_listener("serial0-output-byte", function(byte)
assert("da1fb5b421123c58080a59832675632505b8c139a8d7ecd1c31591ca5c65cea6" === hash.digest("hex"));
console.log("ok");
clearTimeout(timeout);
emulator.stop();
emulator.destroy();
}
});
2 changes: 1 addition & 1 deletion tests/api/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async function run_test(name, config, done)
}

console.log("Done: %s", name);
emulator.stop();
emulator.destroy();
}

(async function() {
Expand Down
2 changes: 1 addition & 1 deletion tests/benchmark/arch-bytemark.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ emulator.add_listener("serial0-output-byte", function(byte)

if(line === "* Trademarks are property of their respective holder.")
{
emulator.stop();
emulator.destroy();

if(BENCH_COLLECT_STATS)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/benchmark/arch-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ emulator.add_listener("serial0-output-byte", function(byte)

if(line.startsWith("sys"))
{
emulator.stop();
emulator.destroy();

if(BENCH_COLLECT_STATS)
{
Expand Down
2 changes: 1 addition & 1 deletion tests/benchmark/linux-boot.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ emulator.add_listener("serial0-output-byte", function(byte)
const end_time = Date.now();
const elapsed = end_time - start_time;
console.log("Done in %dms", elapsed);
emulator.stop();
emulator.destroy();

if(BENCH_COLLECT_STATS)
{
Expand Down
1 change: 0 additions & 1 deletion tests/devices/fetch_network.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ emulator.add_listener("serial0-output-byte", function(byte)

if(test_num >= tests.length)
{
emulator.stop();
emulator.destroy();

console.log("Tests finished.");
Expand Down
2 changes: 1 addition & 1 deletion tests/devices/virtio_console.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ emulator.add_listener("serial0-output-byte", function(byte)
if(line.endsWith("pong"))
{
console.log("\nTest passed");
emulator.stop();
emulator.destroy();
}
});

Expand Down
1 change: 0 additions & 1 deletion tests/devices/wisp_network.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,6 @@ emulator.add_listener("serial0-output-byte", function(byte)

if(test_num >= tests.length)
{
emulator.stop();
emulator.destroy();

console.log("Tests finished.");
Expand Down
1 change: 0 additions & 1 deletion tests/full/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,6 @@ function run_test(test, done)
clearInterval(screen_interval);
}

emulator.stop();
emulator.destroy();

if(test.failure_allowed)
Expand Down
2 changes: 1 addition & 1 deletion tests/jit-paging/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ emulator.add_listener("serial0-output-byte", async function(byte)

const data = await emulator.read_file("/result");

emulator.stop();
emulator.destroy();

let result = Buffer.from(data).toString();
if(result !== "test_shared passed\ntest_consecutive_written passed\n")
Expand Down
1 change: 0 additions & 1 deletion tests/manual/gc.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
});

setTimeout(() => {
emulator.stop();
emulator.destroy();
console.log("Emulator freed. Check using devtools (in chromium: Memory -> Heap Snapshot -> click collect garbage -> take snapshot).");
}, 3 * 1000);
Expand Down
2 changes: 1 addition & 1 deletion tests/qemu/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ emulator.add_listener("serial0-output-byte", async function(byte)
console.error("Got result, writing to stdout");

process.stdout.write(Buffer.from(data));
emulator.stop();
emulator.destroy();
}
});
2 changes: 1 addition & 1 deletion tools/docker/alpine/build-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ emulator.add_listener("serial0-output-byte", function(byte)
{
if(e) throw e;
console.log("Saved as " + OUTPUT_FILE);
emulator.stop();
emulator.destroy();
});
}, 10 * 1000);
}
Expand Down

0 comments on commit 23411f3

Please sign in to comment.