watchfiles
Does Not Correctly Handle Process Exit on Windows
#308
Labels
watchfiles
Does Not Correctly Handle Process Exit on Windows
#308
Description
Problem Description
On Windows,
os.kill(self.pid, signal.SIGINT)
does not behave as expected. Instead of sending a signal that the process can handle (e.g., SIGINT for cleanup), it forcibly terminates the process. This is due to the wayos.kill
is implemented on Windows, where any signal value other thanCTRL_C_EVENT
orCTRL_BREAK_EVENT
results in the process being unconditionally terminated via theTerminateProcess
API.Observed Behavior
When
os.kill(self.pid, signal.SIGINT)
is called in thestop
method:TerminateProcess
, skipping any signal handling logic in the child process.This behavior is inconsistent with Unix-like systems, where
SIGINT
can be caught and handled by the process.Root Cause
On Windows:
os.kill
interpretssignal.SIGINT
as a generic signal value and does not propagate it to the child process in a way that allows signal handlers to execute.os.kill(self.pid, signal.SIGINT)
immediately callsTerminateProcess
, bypassing the child process's ability to handle the signal.The [Python documentation](https://docs.python.org/3/library/os.html#os.kill) confirms this behavior:
Expected Behavior
os.kill(self.pid, signal.SIGINT)
should allow the child process to catch the signal and execute its signal handler (if defined).Suggested Fix
Use
CTRL_C_EVENT
for SIGINT on Windows: Modify thestop
method to useCTRL_C_EVENT
when running on Windows:Example Code
Watchfiles Output
Operating System & Architecture
Windows-11-10.0.22631-SP0
10.0.22631
Environment
No response
Python & Watchfiles Version
python: 3.12.4 (tags/v3.12.4:8e8a4ba, Jun 6 2024, 19:30:16) [MSC v.1940 64 bit (AMD64)], watchfiles: 0.24.0
Rust & Cargo Version
No response
The text was updated successfully, but these errors were encountered: