Skip to content

Commit

Permalink
fix(profiling): do not clear thread-loop link too often (#3668) (#3711)
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.

(cherry picked from commit f583fec)

Co-authored-by: Julien Danjou <julien@danjou.info>
  • Loading branch information
mergify[bot] and jd committed May 13, 2022
1 parent 8bf7055 commit 5e6413e
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
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)
@@ -0,0 +1,4 @@
---
fixes:
- |
Fixes a performance issue with the profiler when used in an asyncio application.

0 comments on commit 5e6413e

Please sign in to comment.