Skip to content

_run_compaction_for_sliding_window task can be garbage collected mid-execution #3174

@cdombroski-google

Description

@cdombroski-google

asyncio.create_task must have a strong reference otherwise they can get garbage collected.

https://docs.python.org/3/library/asyncio-task.html#asyncio.create_task:

Important Save a reference to the result of this function, to avoid a task disappearing mid-execution. The event loop only keeps weak references to tasks. A task that isn’t referenced elsewhere may get garbage collected at any time, even before it’s done. For reliable “fire-and-forget” background tasks, gather them in a collection:

# Run compaction after all events are yielded from the agent.
# (We don't compact in the middle of an invocation, we only compact at the end of an invocation.)
if self.app and self.app.events_compaction_config:
logger.info('Running event compactor.')
# Run compaction in a separate task to avoid blocking the main thread.
# So the users can still finish the event loop from the agent while the
# compaction is running.
asyncio.create_task(
_run_compaction_for_sliding_window(
self.app, session, self.session_service
)
)

Note that the create_task result is not stored.

Metadata

Metadata

Assignees

Labels

core[Component] This issue is related to the core interface and implementation

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions