Skip to content

Commit e170cf7

Browse files
committed
ref(asyncio integration): Refactor code to avoid duplication
GH-4699
1 parent 31ae84b commit e170cf7

File tree

3 files changed

+37
-40
lines changed

3 files changed

+37
-40
lines changed

sentry_sdk/integrations/asyncio.py

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,33 @@ def _patched_close() -> None:
7676
loop._sentry_flush_patched = True # type: ignore
7777

7878

79+
def _create_task_with_factory(
80+
orig_task_factory: Any,
81+
loop: asyncio.AbstractEventLoop,
82+
coro: Coroutine[Any, Any, Any],
83+
**kwargs: Any,
84+
) -> asyncio.Task[Any]:
85+
task = None
86+
87+
# Trying to use user set task factory (if there is one)
88+
if orig_task_factory:
89+
task = orig_task_factory(loop, coro, **kwargs)
90+
91+
if task is None:
92+
# The default task factory in `asyncio` does not have its own function
93+
# but is just a couple of lines in `asyncio.base_events.create_task()`
94+
# Those lines are copied here.
95+
96+
# WARNING:
97+
# If the default behavior of the task creation in asyncio changes,
98+
# this will break!
99+
task = Task(coro, loop=loop, **kwargs)
100+
if task._source_traceback: # type: ignore
101+
del task._source_traceback[-1] # type: ignore
102+
103+
return task
104+
105+
79106
def patch_asyncio() -> None:
80107
orig_task_factory = None
81108
try:
@@ -92,15 +119,9 @@ def _sentry_task_factory(
92119
is_internal = is_internal_task()
93120

94121
if is_internal:
95-
task = None
96-
if orig_task_factory:
97-
task = orig_task_factory(loop, coro, **kwargs)
98-
if task is None:
99-
task = Task(coro, loop=loop, **kwargs)
100-
if task._source_traceback: # type: ignore
101-
del task._source_traceback[-1] # type: ignore
102-
103-
return task
122+
return _create_task_with_factory(
123+
orig_task_factory, loop, coro, **kwargs
124+
)
104125

105126
async def _task_with_sentry_span_creation() -> Any:
106127
result = None
@@ -119,25 +140,9 @@ async def _task_with_sentry_span_creation() -> Any:
119140

120141
return result
121142

122-
task = None
123-
124-
# Trying to use user set task factory (if there is one)
125-
if orig_task_factory:
126-
task = orig_task_factory(
127-
loop, _task_with_sentry_span_creation(), **kwargs
128-
)
129-
130-
if task is None:
131-
# The default task factory in `asyncio` does not have its own function
132-
# but is just a couple of lines in `asyncio.base_events.create_task()`
133-
# Those lines are copied here.
134-
135-
# WARNING:
136-
# If the default behavior of the task creation in asyncio changes,
137-
# this will break!
138-
task = Task(_task_with_sentry_span_creation(), loop=loop, **kwargs)
139-
if task._source_traceback: # type: ignore
140-
del task._source_traceback[-1] # type: ignore
143+
task = _create_task_with_factory(
144+
orig_task_factory, loop, _task_with_sentry_span_creation(), **kwargs
145+
)
141146

142147
# Set the task name to include the original coroutine's name
143148
try:

sentry_sdk/transport.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ def kill(self: Self) -> Optional[asyncio.Task[None]]: # type: ignore
806806
try:
807807
# Return the pool cleanup task so caller can await it if needed
808808
with mark_sentry_task_internal():
809-
return self.loop.create_task(self._pool.aclose(), name="sentry_sdk_pool_aclose") # type: ignore
809+
return self.loop.create_task(self._pool.aclose()) # type: ignore
810810
except RuntimeError:
811811
logger.warning("Event loop not running, aborting kill.")
812812
return None

sentry_sdk/worker.py

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,7 @@ def start(self) -> None:
232232
if self._queue is None:
233233
self._queue = asyncio.Queue(maxsize=self._queue_size)
234234
with mark_sentry_task_internal():
235-
self._task = self._loop.create_task(
236-
self._target(), name="sentry_sdk_async_worker"
237-
)
235+
self._task = self._loop.create_task(self._target())
238236
self._task_for_pid = os.getpid()
239237
except RuntimeError:
240238
# There is no event loop running
@@ -277,10 +275,7 @@ async def _wait_flush(self, timeout: float, callback: Optional[Any] = None) -> N
277275
def flush(self, timeout: float, callback: Optional[Any] = None) -> Optional[asyncio.Task[None]]: # type: ignore[override]
278276
if self.is_alive and timeout > 0.0 and self._loop and self._loop.is_running():
279277
with mark_sentry_task_internal():
280-
return self._loop.create_task(
281-
self._wait_flush(timeout, callback),
282-
name="sentry_sdk_async_worker_flush",
283-
)
278+
return self._loop.create_task(self._wait_flush(timeout, callback))
284279
return None
285280

286281
def submit(self, callback: Callable[[], Any]) -> bool:
@@ -303,10 +298,7 @@ async def _target(self) -> None:
303298
break
304299
# Firing tasks instead of awaiting them allows for concurrent requests
305300
with mark_sentry_task_internal():
306-
task = asyncio.create_task(
307-
self._process_callback(callback),
308-
name="sentry_sdk_async_worker_process_callback",
309-
)
301+
task = asyncio.create_task(self._process_callback(callback))
310302
# Create a strong reference to the task so it can be cancelled on kill
311303
# and does not get garbage collected while running
312304
self._active_tasks.add(task)

0 commit comments

Comments
 (0)