Skip to content

Commit

Permalink
fix(profiling): do not clear thread-loop link too often (#3668)
Browse files Browse the repository at this point in the history
The current code calls DdtraceProfilerEventLoopPolicy.clear_threads each time a
task is resolved, which can be up to 100 times a second. This is way too much
and we don't expect for the thread->loop mapping to change that often.

Instead we often try to clear the mapping when a new loop is attached to a
thread. In a regular application, this is the expected workflow: a thread
appears and gets a new loop, so we clear the old ones.

The worst case scenario would be an app spawning 100 threads with 100 loops and
then not doing that ever again, which would make the profiler keep a reference
on the 100 loops — until a new loop is attached, which if never, would kept the
reference forever.

Since this far from being a common pattern, it should be safe to switch to a
simpler model like this.
  • Loading branch information
jd committed May 12, 2022
1 parent ebe73a8 commit f583fec
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
1 change: 0 additions & 1 deletion ddtrace/profiling/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,4 @@ def set_event_loop(self, loop):

def _ddtrace_get_loop(self, thread_id):
# type: (...) -> typing.Optional[asyncio.AbstractEventLoop]
self._clear_threads()
return self.loop_per_thread.get_object(thread_id)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
Fixes a performance issue with the profiler when used in an asyncio application.

0 comments on commit f583fec

Please sign in to comment.