While I am still uncertain regarding the precise cause of the problem, I have some additional observations as well as a workaround.
Workaround.
Adding a try-except
block in the finally clause.
finally:
try:
proc.terminate()
except WindowsError:
pass
This also seems to be the solution arrived at in a related (?) issue posted here on GitHub (you may have to scroll down a bit).
Observations.
- This error is dependent on the size of the object passed to the Process/Queue, but it is not related to the execution of the Process itself. In the OP, the Process completes before the timeout expires.
proc.is_alive
returnsTrue
before and after the execution ofproc.terminate()
(which then throws the WindowsError). A second or two later,proc.is_alive()
returnsFalse
and a second call toproc.terminate()
succeeds.- Forcing the main thread to sleep
time.sleep(1)
in thefinally
block also prevents the throwing of the WindowsError. Thanks, @tdelaney's comment in the OP. - My best guess is that
proc
is in the process of freeing memory (?, or something comparable) while being killed by the OS (having completed execution) when the call toproc.terminate()
attempts to kill it again.