Commit e4e3395
committed
Detach pipe handles from Popen after timeout to prevent __del__ race
After a subprocess timeout on Windows, the reader threads started by
communicate() may still be draining buffered pipe output even after
taskkill and proc.kill(). If they take longer than the 5-second join
window, proc.__del__() runs while a thread is in fh.read() and closes
the handle under it. That raises OSError in the thread, which triggers
pytest's threading.excepthook and produces exit code 1 despite zero test
failures.
Setting proc.stdout = None and proc.stderr = None before returning
prevents __del__() from seeing those handles. Each reader thread holds
its own local reference to the file object (bound at Thread creation),
reads until EOF, and calls fh.close() itself. No handle is leaked; the
race is eliminated regardless of how long the drain takes.1 parent 184e754 commit e4e3395
1 file changed
Lines changed: 12 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
856 | 856 | | |
857 | 857 | | |
858 | 858 | | |
859 | | - | |
860 | | - | |
861 | | - | |
862 | | - | |
863 | | - | |
864 | | - | |
| 859 | + | |
865 | 860 | | |
866 | 861 | | |
867 | 862 | | |
868 | 863 | | |
869 | 864 | | |
| 865 | + | |
| 866 | + | |
| 867 | + | |
| 868 | + | |
| 869 | + | |
| 870 | + | |
| 871 | + | |
| 872 | + | |
| 873 | + | |
| 874 | + | |
| 875 | + | |
870 | 876 | | |
871 | 877 | | |
872 | 878 | | |
| |||
0 commit comments