Netflix-Skunkworks / raven-python-lambda

Sentry/Raven SDK Integration For AWS Lambda (python) and Serverless
Apache License 2.0
47 stars 15 forks source link

Cancel threads at the end of execution #12

Closed bjorn-spire closed 7 years ago

bjorn-spire commented 7 years ago

Because lambda doesn't actually stop the Python processes proper after each execution the threads would linger around after finishing. After enough invocations of your function it would start failing from:

  error: can't start new thread
   File "raven_python_lambda/__init__.py", line 176, in decorated
     install_timers(self.config, context)
   File "raven_python_lambda/__init__.py", line 231, in install_timers
     Timer(max(time_remaining - 500, 0), timeout_error, (config)).start()
   File "python2.7/threading.py", line 739, in start
     _start_new_thread(self.__bootstrap, ())  

By collecting all the timers and then canceling them at the end of function execution we're no longer seeing this resource constraint.

Also fixed a completely unrelated bug with time_remaining. While writing the test I got an invocation of it and saw that it couldn't run because the config object was splatted out to 8 arguments instead of the one config as expected. :)

bjorn-spire commented 7 years ago

Oh, to validate that the threads aren't killed between invocations I created a new lambda and ran it a couple of times and saw that the count went up:

from threading import Timer, active_count

def handler(event, context):
    def end_of_timer():
        print('end of timer!')

    t = Timer(290, end_of_timer)
    t.start()

    print('Currently active threads: {}'.format(active_count()))
kevgliss commented 7 years ago

Thats a good point. We hadn't noticed this previously.