Skip to content
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

Gracefully error out when there are concurrent profilers #692

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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
Loading