Answer
Electron does not provide a built‑in timeout argument for ipc.invoke. The method returns a plain Promise that resolves or rejects only when the handler finishes.
Confirmed facts
- The
ipc.invoke API signature is invoke(channel, ...args) with no optional timeout parameter.
- As of the latest stable Electron release (30.x, 2024) the open issue requesting a timeout flag remains unmerged.
- Wrapping the call in
Promise.race with a timer Promise will reject the caller’s side after the timeout, but the original handler continues to run in its process.
Likely explanation
Because Electron’s IPC is designed to be fire‑and‑forget on the sender side, adding a timeout would require a cancellation mechanism on the receiver side, which does not exist today. The race‑only approach therefore leaves the handler to finish its work, potentially wasting CPU, I/O, or blocking threads.
Steps to avoid resource waste when using Promise.race
- Introduce a cancellation signal – create an
AbortController in the renderer and pass its signal to the handler via an extra argument or a shared message.
- Check the signal inside the handler – at appropriate breakpoints, test
signal.aborted and return early or throw to stop further work.
- Offload heavy work – move blocking or I/O‑intensive tasks to a
Worker thread or a separate child process so that terminating the worker frees resources.
- Use transferable objects – if you pass large buffers, mark them as transferable to avoid unnecessary copies when the operation is cancelled.
- Clean up after the race – in the
catch branch of the Promise.race, ensure any allocated resources (e.g., file handles, timers) are released.
Diagnostic question
Can you modify the IPC handler (the main‑process function that receives the ipc.invoke call) to check for an abort signal? If yes, the AbortController pattern above is the recommended approach; if not, you must rely on offloading to a worker and terminating that worker when the timeout occurs.