| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 327b927 commit 25189bc
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,15 +1,26 @@ | |||
| 1 | 1 | import { isMainThread, Worker } from 'node:worker_threads'; | |
| 2 | 2 | ||
| 3 | + const registeredRefs = []; | ||
| 4 | + | ||
| 5 | + const ref = { foo: 'foo' }; | ||
| 6 | + registeredRefs.push(ref); | ||
| 7 | + | ||
| 3 | 8 | if (isMainThread) { | |
| 4 | - process.finalization.register({ foo: 'foo' }, () => { | ||
| 9 | + process.finalization.register(ref, () => { | ||
| 5 | 10 | process.stdout.write('shutdown on main thread\n'); | |
| 6 | 11 | }); | |
| 7 | 12 | ||
| 8 | 13 | const worker = new Worker(import.meta.filename); | |
| 9 | 14 | ||
| 15 | + worker.on('error', (err) => { | ||
| 16 | + // Referencing `registeredRefs` here to avoid `ref` being GCed before the worker exits. | ||
| 17 | + console.log(registeredRefs); | ||
| 18 | + throw err; | ||
| 19 | + }); | ||
| 20 | + | ||
| 10 | 21 | worker.postMessage('ping'); | |
| 11 | 22 | } else { | |
| 12 | - process.finalization.register({ foo: 'bar' }, () => { | ||
| 23 | + process.finalization.register(ref, () => { | ||
| 13 | 24 | process.stdout.write('shutdown on worker\n'); | |
| 14 | 25 | }); | |
| 15 | 26 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments