Skip to content

Commit ee44621

Browse files
committed
ref(worker): Add queue as optional to allow for initialisation in start
GH-4581
1 parent 9e380b8 commit ee44621

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

sentry_sdk/worker.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def _target(self) -> None:
191191

192192
class AsyncWorker(Worker):
193193
def __init__(self, queue_size: int = DEFAULT_QUEUE_SIZE) -> None:
194-
self._queue: asyncio.Queue[Any] = asyncio.Queue(maxsize=queue_size)
194+
self._queue: Optional[asyncio.Queue[Any]] = None
195195
self._queue_size = queue_size
196196
self._task: Optional[asyncio.Task[None]] = None
197197
# Event loop needs to remain in the same process
@@ -210,10 +210,11 @@ def is_alive(self) -> bool:
210210

211211
def kill(self) -> None:
212212
if self._task:
213-
try:
214-
self._queue.put_nowait(_TERMINATOR)
215-
except asyncio.QueueFull:
216-
logger.debug("async worker queue full, kill failed")
213+
if self._queue is not None:
214+
try:
215+
self._queue.put_nowait(_TERMINATOR)
216+
except asyncio.QueueFull:
217+
logger.debug("async worker queue full, kill failed")
217218
# Also cancel any active callback tasks
218219
# Avoid modifying the set while cancelling tasks
219220
tasks_to_cancel = set(self._active_tasks)
@@ -240,14 +241,16 @@ def start(self) -> None:
240241
self._task_for_pid = None
241242

242243
def full(self) -> bool:
244+
if self._queue is None:
245+
return True
243246
return self._queue.full()
244247

245248
def _ensure_task(self) -> None:
246249
if not self.is_alive:
247250
self.start()
248251

249252
async def _wait_flush(self, timeout: float, callback: Optional[Any] = None) -> None:
250-
if not self._loop or not self._loop.is_running():
253+
if not self._loop or not self._loop.is_running() or self._queue is None:
251254
return
252255

253256
initial_timeout = min(0.1, timeout)
@@ -276,14 +279,17 @@ async def flush_async(self, timeout: float, callback: Optional[Any] = None) -> N
276279

277280
def submit(self, callback: Callable[[], Any]) -> bool:
278281
self._ensure_task()
279-
282+
if self._queue is None:
283+
return False
280284
try:
281285
self._queue.put_nowait(callback)
282286
return True
283287
except asyncio.QueueFull:
284288
return False
285289

286290
async def _target(self) -> None:
291+
if self._queue is None:
292+
return
287293
while True:
288294
callback = await self._queue.get()
289295
if callback is _TERMINATOR:
@@ -310,5 +316,6 @@ def _on_task_complete(self, task: asyncio.Task[None]) -> None:
310316
finally:
311317
# Mark the task as done and remove it from the active tasks set
312318
# This happens only after the task has completed
313-
self._queue.task_done()
319+
if self._queue is not None:
320+
self._queue.task_done()
314321
self._active_tasks.discard(task)

0 commit comments

Comments
 (0)