◐ Shell
clean mode source ↗

Message 162054 - Python tracker

>  conn = MySQLConn()
>  start_thread1(conn)
>  start_thread2(conn):
>  while True:
>    if os.fork() == 0:  # child
>      raise Exception('doom')  # triggers destructor

There is no guarantee here that the lock will be held at the time of the fork.  So even if we ensure that a lock acquired before the fork stayed lock, we won't necessarily get a deadlock.

More importantly, you should never fork without ensuring that you exit with os._exit() or os.exec*().  So your example should be something like

  conn = MySQLConn()
  start_thread1(conn)
  start_thread2(conn):
  while True:
    if os.fork() == 0:  # child
      try:
        raise Exception('doom')  # does NOT trigger destructor
      except:
        sys.excepthook(*sys.exc_info())
        os._exit(1)
      else:
        os._exit(0)

With this hard exit the destructor never runs.