Skip to content

Event reporting within child process does not work without time.sleep #473

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
vzts opened this issue Aug 19, 2019 · 3 comments
Closed

Event reporting within child process does not work without time.sleep #473

vzts opened this issue Aug 19, 2019 · 3 comments

Comments

@vzts
Copy link

vzts commented Aug 19, 2019

import sentry_sdk
from multiprocessing import Process
from time import sleep


def child_process():
    try:
        test_child = 1 / 0
    except Exception as e:
        sentry_sdk.capture_exception(e)
        sleep(1)  # I have to give this sleep


sentry_sdk.init(
    '...',
    debug=True
)
proc = Process(target=child_process)
proc.start()
proc.join()

Without giving sleep, the event reporting is not correctly triggered. Why is this? Is it because the worker thread is a daemon thread? (but I have tried to comment self._thread.setDaemon(True) out, but it still shows the same symptom)

@vzts vzts changed the title Event reporting in sub process works in an unexpected way Event reporting in sub process does not work without time.sleep Aug 19, 2019
@vzts vzts changed the title Event reporting in sub process does not work without time.sleep Event reporting within sub process does not work without time.sleep Aug 19, 2019
@vzts vzts changed the title Event reporting within sub process does not work without time.sleep Event reporting within sub process does not work without time.sleep Aug 19, 2019
@vzts vzts changed the title Event reporting within sub process does not work without time.sleep Event reporting within sub process does not work without time.sleep Aug 19, 2019
@untitaker
Copy link
Member

multiprocessing introduces odd interpreter behavior for subprocesses. See #291.

Your best fix is to add sentry_sdk.Hub.current.flush() after capture_exception(), which ensures that the process sleeps as long as it's necessary to send events out.

For the main process this is done automatically via atexit, but those hooks do not run in multiprocessing.

@vzts
Copy link
Author

vzts commented Aug 19, 2019

@untitaker Thank you for your quick response! (probably the fastest I received in github)
All make sense to me and the flushing did resolve the issue.
Thanks again for the explanation :)

@vzts vzts closed this as completed Aug 19, 2019
@vzts vzts changed the title Event reporting within sub process does not work without time.sleep Event reporting within child process does not work without time.sleep Aug 19, 2019
@untitaker
Copy link
Member

See this question: https://stackoverflow.com/a/34507557/1544347

It's very unfortunate behavior. We even work around this in the Celery integration, but the fix is Celery-specific (and has to be).

Theoretically you could monkeypatch os._exit to make things work "automatically", but that's dangerous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants