Skip to content

Instantly share code, notes, and snippets.

@msukmanowsky
Created July 4, 2017 20:24
Show Gist options
  • Save msukmanowsky/b440375ec95915c609e963b63ad37e4f to your computer and use it in GitHub Desktop.
Save msukmanowsky/b440375ec95915c609e963b63ad37e4f to your computer and use it in GitHub Desktop.
Demonstrating issues between sys.exit and os._exit when active threads are running.
import logging
import os
import sys
import threading
import time
import six
logging.basicConfig(level=logging.INFO, format='%(name)s %(threadName)s %(thread)d [%(levelname)s]: %(message)s')
def bad_func(shared_state):
time.sleep(1)
try:
raise Exception('This is a stupid')
except:
shared_state['exc_info'] = sys.exc_info()
def good_func():
while True:
time.sleep(1)
logging.info('All good!')
logging.info('Python Version: %s', sys.version)
logging.info('PID: %s', os.getpid())
bad_state = {}
bad_thread = threading.Thread(target=bad_func, name='bad_thread', args=(bad_state,))
logging.info('bad_thread daemon: %r', bad_thread.daemon)
good_thread = threading.Thread(target=good_func, name='good_thread')
logging.info('good_thread daemon: %r', bad_thread.daemon)
good_thread.start()
bad_thread.start()
logging.info('Thread count: %d', threading.active_count())
try:
while True:
exc_info = bad_state.get('exc_info')
if exc_info:
six.reraise(*exc_info)
good_thread.join(0.1)
bad_thread.join(0.1)
except Exception as e:
logging.error('Caught an exception, exiting: %r', e)
sys.exit(2)
logging.info('Done from main.')
root MainThread 140736469173184 [INFO]: Python Version: 2.7.11 (default, Jul 19 2016, 10:14:23)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)]
root MainThread 140736469173184 [INFO]: PID: 30663
root MainThread 140736469173184 [INFO]: bad_thread daemon: False
root MainThread 140736469173184 [INFO]: good_thread daemon: False
root MainThread 140736469173184 [INFO]: Thread count: 3
root good_thread 123145383870464 [INFO]: All good!
root MainThread 140736469173184 [ERROR]: Caught an exception, exiting: Exception('This is a stupid',)
root good_thread 123145383870464 [INFO]: All good!
root good_thread 123145383870464 [INFO]: All good!
root good_thread 123145383870464 [INFO]: All good!
root good_thread 123145383870464 [INFO]: All good!
root good_thread 123145383870464 [INFO]: All good!
root good_thread 123145383870464 [INFO]: All good!
root good_thread 123145383870464 [INFO]: All good!
root good_thread 123145383870464 [INFO]: All good!
root good_thread 123145383870464 [INFO]: All good!
...
import logging
import os
import sys
import threading
import time
import six
logging.basicConfig(level=logging.INFO, format='%(name)s %(threadName)s %(thread)d [%(levelname)s]: %(message)s')
def bad_func(shared_state):
time.sleep(1)
try:
raise Exception('This is a stupid')
except:
shared_state['exc_info'] = sys.exc_info()
def good_func():
while True:
time.sleep(1)
logging.info('All good!')
logging.info('Python Version: %s', sys.version)
logging.info('PID: %s', os.getpid())
bad_state = {}
bad_thread = threading.Thread(target=bad_func, name='bad_thread', args=(bad_state,))
logging.info('bad_thread daemon: %r', bad_thread.daemon)
good_thread = threading.Thread(target=good_func, name='good_thread')
logging.info('good_thread daemon: %r', bad_thread.daemon)
good_thread.start()
bad_thread.start()
logging.info('Thread count: %d', threading.active_count())
try:
while True:
exc_info = bad_state.get('exc_info')
if exc_info:
six.reraise(*exc_info)
good_thread.join(0.1)
bad_thread.join(0.1)
except Exception as e:
logging.error('Caught an exception, exiting: %r', e)
exit_func = os._exit if threading.active_count() > 1 else sys.exit()
exit_func(2)
logging.info('Done from main.')
root MainThread 140736469173184 [INFO]: Python Version: 2.7.11 (default, Jul 19 2016, 10:14:23)
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)]
root MainThread 140736469173184 [INFO]: PID: 30554
root MainThread 140736469173184 [INFO]: bad_thread daemon: False
root MainThread 140736469173184 [INFO]: good_thread daemon: False
root MainThread 140736469173184 [INFO]: Thread count: 3
root good_thread 123145366458368 [INFO]: All good!
root MainThread 140736469173184 [ERROR]: Caught an exception, exiting: Exception('This is a stupid',)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment