Skip to content

Commit 6537b6e

Browse files
committed
Gracefully error out when there are concurrent profilers
1 parent 23ff43b commit 6537b6e

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

project/tests/test_collector.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import cProfile
2+
import sys
3+
14
from django.test import TestCase
25
from tests.util import DictStorage
36

@@ -47,6 +50,18 @@ def test_finalise(self):
4750
self.assertTrue(content)
4851
self.assertGreater(len(content), 0)
4952

53+
def test_configure_exception(self):
54+
other_profiler = cProfile.Profile()
55+
other_profiler.enable()
56+
collector = DataCollector()
57+
collector.configure()
58+
other_profiler.disable()
59+
if sys.version_info >= (3, 12):
60+
self.assertEqual(collector.local.pythonprofiler, None)
61+
else:
62+
self.assertIsNotNone(collector.local.pythonprofiler)
63+
collector.stop_python_profiler()
64+
5065
def test_profile_file_name_with_disabled_extended_file_name(self):
5166
SilkyConfig().SILKY_PYTHON_PROFILER_EXTENDED_FILE_NAME = False
5267
request_path = 'normal/uri/'

silk/collector.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ def configure(self, request=None, should_profile=True):
9292
self._configure()
9393
if should_profile:
9494
self.local.pythonprofiler = cProfile.Profile()
95-
self.local.pythonprofiler.enable()
95+
try:
96+
self.local.pythonprofiler.enable()
97+
except ValueError as e:
98+
# Deal with cProfile not being allowed to run concurrently
99+
# https://github.com/jazzband/django-silk/issues/682
100+
Logger.error('Could not enable python profiler, %s' % str(e), exc_info=True)
101+
self.local.pythonprofiler = None
96102

97103
def clear(self):
98104
self.request = None

0 commit comments

Comments
 (0)