Skip to content
Myr

strict.js

This file

Path

lib/ipc/strict.js

129 lines · 4.3 KiB · JavaScript

Last changed by

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

History of this file

Take it

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 1–4why, one step back
1import {once} from 'node:events';
2import {createDeferred} from '../utils/deferred.js';3import {incrementMaxListeners} from '../utils/max-listeners.js';4import {sendMessage} from './send.js';

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

line 5why, one step back
5import {throwOnMissingStrict, throwOnStrictDisconnect, throwOnStrictDeadlockError} from './validation.js';

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 6–14why, one step back
6import {getIpcEmitter} from './forward.js';
7import {hasMessageListeners} from './outgoing.js';89// When using the `strict` option, wrap the message with metadata during `sendMessage()`10export const handleSendStrict = ({anyProcess, channel, isSubprocess, message, strict}) => {11 if (!strict) {12 return message;13 }14

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

lines 15–22why, one step back
15 const ipcEmitter = getIpcEmitter(anyProcess, channel, isSubprocess);
16 const hasListeners = hasMessageListeners(anyProcess, ipcEmitter);17 return {18 id: count++,19 type: REQUEST_TYPE,20 message,21 hasListeners,22 };

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 23–24why, one step back
23};
24

Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a

lines 25–26why, one step back
25// This is a plain number, not a `BigInt`, since it must be serializable with `serialization: 'json'`
26let count = 0;

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

line 27why, one step back
27

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

lines 28–30why, one step back
28// Handles when both processes are calling `sendMessage()` with `strict` at the same time.
29// If neither process is listening, this would create a deadlock. We detect it and throw.30export const validateStrictDeadlock = (outgoingMessages, wrappedMessage) => {

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

line 31why, one step back
31 if (!isStrictRequest(wrappedMessage) || wrappedMessage.hasListeners) {

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

lines 32–36why, one step back
32 return;
33 }3435 for (const {id} of outgoingMessages) {36 if (id !== undefined) {

TweaksSindre Sorhusbefore adoption698a1b8

line 37why, one step back
37 STRICT_RESPONSES[id].deferred.resolve({isDeadlock: true, hasListeners: false});

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

lines 38–41why, one step back
38 }
39 }40};41

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 42–43why, one step back
42// The other process then sends the acknowledgment back as a response
43export const handleStrictRequest = async ({wrappedMessage, anyProcess, channel, isSubprocess, ipcEmitter}) => {

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

line 44why, one step back
44 if (!isStrictRequest(wrappedMessage)) {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 45–48why, one step back
45 return wrappedMessage;
46 }4748 const {id, message} = wrappedMessage;

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 49–53why, one step back
49 // The acknowledgment cannot be sent anymore, but the message is still delivered
50 if (!anyProcess.connected) {51 return message;52 }53

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 54–63why, one step back
54 const response = {id, type: RESPONSE_TYPE, message: hasMessageListeners(anyProcess, ipcEmitter)};
5556 try {57 await sendMessage({58 anyProcess,59 channel,60 isSubprocess,61 ipc: true,62 }, response);63 } catch (error) {

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 64–67why, one step back
64 // When the other process disconnects right after sending the message, it cannot receive the acknowledgment anymore. Like above, the message is still delivered.
65 if (error.cause?.code !== 'EPIPE') {66 ipcEmitter.emit('strict:error', error);67 }

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 68–72why, one step back
68 }
6970 return message;71};72

TweaksSindre Sorhusbefore adoption698a1b8

lines 73–75why, one step back
73// Reception of the acknowledgment response.
74// Ids are shared by all processes, so the response must come from the process the message was sent to. Otherwise, a subprocess could acknowledge a message sent to another one.75export const handleStrictResponse = (wrappedMessage, anyProcess) => {

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 76–80why, one step back
76 if (wrappedMessage?.type !== RESPONSE_TYPE) {
77 return false;78 }7980 const {id, message: hasListeners} = wrappedMessage;

TweaksSindre Sorhusbefore adoption698a1b8

lines 81–85why, one step back
81 const strictResponse = STRICT_RESPONSES[id];
82 if (strictResponse?.anyProcess === anyProcess) {83 strictResponse.deferred.resolve({isDeadlock: false, hasListeners});84 }85

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 86–91why, one step back
86 return true;
87};8889// Wait for the other process to receive the message from `sendMessage()`90export const waitForStrictResponse = async (wrappedMessage, anyProcess, isSubprocess) => {91 const deferred = createDeferred();

TweaksSindre Sorhusbefore adoption698a1b8

line 92why, one step back
92 STRICT_RESPONSES[wrappedMessage.id] = {deferred, anyProcess};

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

line 93why, one step back
93 const controller = new AbortController();

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 94–95why, one step back
94
95 try {

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

line 96why, one step back
96 const {isDeadlock, hasListeners} = await Promise.race([

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 97–100why, one step back
97 deferred,
98 throwOnDisconnect(anyProcess, isSubprocess, controller),99 ]);100

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

lines 101–104why, one step back
101 if (isDeadlock) {
102 throwOnStrictDeadlockError(isSubprocess);103 }104

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 105–108why, one step back
105 if (!hasListeners) {
106 throwOnMissingStrict(isSubprocess);107 }108 } finally {

Fix deadlocks with IPC (#1104)ehmickybefore adoption465e427

line 109why, one step back
109 controller.abort();

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 110–113why, one step back
110 delete STRICT_RESPONSES[wrappedMessage.id];
111 }112};113

Harden some thingsSindre Sorhusbefore adoption4a0a121

line 114why, one step back
114const STRICT_RESPONSES = {__proto__: null};

Add `strict` option to `sendMessage()` (#1098)ehmickybefore adoptione085b10

lines 115–123why, one step back
115
116const throwOnDisconnect = async (anyProcess, isSubprocess, {signal}) => {117 incrementMaxListeners(anyProcess, 1, signal);118 await once(anyProcess, 'disconnect', {signal});119 throwOnStrictDisconnect(isSubprocess);120};121122const REQUEST_TYPE = 'execa:ipc:request';123const RESPONSE_TYPE = 'execa:ipc:response';

Fix IPC edge casesSindre Sorhusbefore adoption5f80cde

lines 124–129why, one step back
124
125// `id` and `hasListeners` are always defined in messages sent by `sendMessage()` with the `strict` option.126// Messages with the same `type` but without them are user messages, which must be delivered as is.127const isStrictRequest = wrappedMessage => wrappedMessage?.type === REQUEST_TYPE128 && Number.isInteger(wrappedMessage.id)129 && typeof wrappedMessage.hasListeners === 'boolean';