Skip to content

Commit

Permalink
Gracefully error out when there are concurrent profilers
Browse files Browse the repository at this point in the history
  • Loading branch information
albertyw committed Dec 30, 2023
1 parent 23ff43b commit 6537b6e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
15 changes: 15 additions & 0 deletions project/tests/test_collector.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import cProfile
import sys

from django.test import TestCase
from tests.util import DictStorage

Expand Down Expand Up @@ -47,6 +50,18 @@ def test_finalise(self):
self.assertTrue(content)
self.assertGreater(len(content), 0)

def test_configure_exception(self):
other_profiler = cProfile.Profile()
other_profiler.enable()
collector = DataCollector()
collector.configure()
other_profiler.disable()
if sys.version_info >= (3, 12):
self.assertEqual(collector.local.pythonprofiler, None)
else:
self.assertIsNotNone(collector.local.pythonprofiler)
collector.stop_python_profiler()

def test_profile_file_name_with_disabled_extended_file_name(self):
SilkyConfig().SILKY_PYTHON_PROFILER_EXTENDED_FILE_NAME = False
request_path = 'normal/uri/'
Expand Down
8 changes: 7 additions & 1 deletion silk/collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,13 @@ def configure(self, request=None, should_profile=True):
self._configure()
if should_profile:
self.local.pythonprofiler = cProfile.Profile()
self.local.pythonprofiler.enable()
try:
self.local.pythonprofiler.enable()
except ValueError as e:

Check warning on line 97 in silk/collector.py

View check run for this annotation

Codecov / codecov/patch

silk/collector.py#L97

Added line #L97 was not covered by tests
# Deal with cProfile not being allowed to run concurrently
# https://github.com/jazzband/django-silk/issues/682
Logger.error('Could not enable python profiler, %s' % str(e), exc_info=True)
self.local.pythonprofiler = None

Check warning on line 101 in silk/collector.py

View check run for this annotation

Codecov / codecov/patch

silk/collector.py#L100-L101

Added lines #L100 - L101 were not covered by tests

def clear(self):
self.request = None
Expand Down

0 comments on commit 6537b6e

Please sign in to comment.