question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sentry and multiprocessing?

See original GitHub issue

I can’t get multiprocessing spawned worker functions to work with sentry when the worker functions raises an exception.

With below code, I expect both of the spawned worker_function to separately send logs to sentry when the ZeroDivisionError is raised. However, sentry doesn’t appear to send any logs at all. Am I missing something, or this is not how it works. Cheers.

import multiprocessing

dsn = 'https://xxx:yyy@sentry.io/zzzz'

def worker_function():
    from raven import Client
    client = Client(dsn)
    print("worker crashes")
    1 / 0

def manager_function():
    workers_count = 2
    workers = [multiprocessing.Process(target=worker_function) for i in range(workers_count)]
    for worker in workers:
        worker.start()

manager_function()

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
hitmeupallthetimecommented, Dec 14, 2017

A solution can be found below. I monkeypatched multiprocessing run, then used the synchronous transport (the async doesn’t work, because it needs to send the pending request when the process ends, but the hook isn’t there).

import multiprocessing
import threading
import sys
from raven import Client
from raven.transport.requests import HTTPTransport

dsn = "xx"


def install_thread_excepthook():
    """
    Workaround for sys.excepthook thread bug
    (https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1230540&group_id=5470).
    Call once from __main__ before creating any threads.
    If using psyco, call psycho.cannotcompile(threading.Thread.run)
    since this replaces a new-style class method.
    """
    start_old = multiprocessing.Process.run
    def run(*args, **kwargs):
        client = Client(dsn, transport=HTTPTransport)
        try:
            start_old(*args, **kwargs)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            client.captureException()
    multiprocessing.Process.run = run

install_thread_excepthook()

def worker_function():

    print("worker crashes")
    1 / 0

def manager_function():
    workers_count = 2
    workers = [multiprocessing.Process(target=worker_function) for i in range(workers_count)]
    for worker in workers:
        worker.start()

manager_function()
2reactions
ashwoodscommented, Oct 28, 2017

Sentry handling exceptions by default works by using sys.excepthook, which doesn’t work with threading and multiprocessing due to python issue 1230540. Workarounds exist but a bit tricky. Wrapping your code in a try and using client.CaptureException should work fine. A long term fix is already on our radar.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to configure sentry to send exception from process ...
Sentry works on an async event based model and killing a process just ... captureException() time.sleep(10) import multiprocessing from ...
Read more >
Multi-Process Apps for Android - Sentry Documentation
Learn how to automatically initialize the SDK in non-main processes.
Read more >
Leave a reply - Server 24/7
/snap/sentry/usr/lib/python2.7/multiprocessing/synchronize.py”, line 75, in __init__ sl = self._semlock = _multiprocessing.SemLock(kind, value, ...
Read more >
2031293 – Review Request: python-django-q - Red Hat Bugzilla
Bug 2031293 - Review Request: python-django-q - A multiprocessing ... SQS, MongoDB or ORM - Rollbar and Sentry support Fedora Account System ...
Read more >
multiprocessing-utils - PyPI
Multiprocessing utilities. Shared locks. “Shared” version of the standard Lock() and RLock() classes found in the multiprocessing/threading modules.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found