-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: clean up handles in worker_threads environments to prevent aborting
- Loading branch information
Showing
8 changed files
with
74 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { addHook } from './addon' | ||
import * as workerThreads from 'worker_threads' | ||
|
||
interface ClosableSocket { | ||
close(): void | ||
} | ||
|
||
const socketSet = new Set<ClosableSocket>() | ||
|
||
export function addSocket(socket: ClosableSocket) { | ||
socketSet.add(socket) | ||
} | ||
|
||
export function deleteSocket(socket: ClosableSocket) { | ||
socketSet.delete(socket) | ||
} | ||
|
||
// Cleanup for worker_threads environment. Otherwise Node.js will abort because there are | ||
// open uv sockets while exiting. | ||
function cleanup() { | ||
const sockets = socketSet.values() | ||
|
||
let next = sockets.next(); | ||
while (!next.done) { | ||
const socket = next.value | ||
socket.close() | ||
next = sockets.next(); | ||
} | ||
|
||
socketSet.clear() | ||
} | ||
|
||
if (!workerThreads.isMainThread) { | ||
addHook(cleanup) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters