Skip to content

Instantly share code, notes, and snippets.

@CyrusF
Last active December 26, 2023 13:32
Show Gist options
  • Select an option

  • Save CyrusF/b57dfab81f76c19e9a3374bf390bdd55 to your computer and use it in GitHub Desktop.

Select an option

Save CyrusF/b57dfab81f76c19e9a3374bf390bdd55 to your computer and use it in GitHub Desktop.
magic python "print" deadlock
import faulthandler
from queue import Queue
import multiprocessing
import os
import sys
import threading
import time
def proc():
faulthandler.dump_traceback_later(1.0, exit=True)
if __name__ == "__main__":
queue = Queue()
def consume():
sys.stdout = sys.stdout # deadlock
#sys.stdout = sys.stderr # deadlock
#sys.stdout = open(f"/proc/{os.getpid()}/fd/1", "w") # deadlock
#sys.stdout = open(f"/proc/{os.getpid()}/fd/2", "w") # deadlock
#sys.stdout = open(f"/proc/{os.getpid()}/fd/3", "w") # no deadlock
#sys.stdout = open("/dev/null", "w") # no deadlock
#sys.stdout = open("/home/myhome/1", "w") # no deadlock
while True:
print("Consumed:", queue.get())
thread = threading.Thread(target=consume)
thread.start()
for i in range(10000):
queue.put(i)
# p = multiprocessing.Process(target=lambda: None)
p = multiprocessing.Process(target=proc)
p.start()
p.join()
if p.exitcode != 0:
print("Child process failed!")
os._exit(0)
print("Not hanging yet", i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment