Skip to content

[FEATURE]: Expose FIRE_AND_FORGET task handles on PluginResult for deterministic awaiting #25

Closed

Description

@ajbozarth

Summary

PluginExecutor._fire_and_forget_tasks() schedules background work via
asyncio.create_task() but discards the resulting task handles. Callers of
PluginManager.invoke_hook() have no way to know when those background tasks
complete. The only workaround today is an arbitrary asyncio.sleep(), which
is racy under load or on slow CI runners.

User Story 1

  • As a: developer using CPEX's FIRE_AND_FORGET plugin mode
  • I want: the task handles created by _fire_and_forget_tasks() to be accessible on PluginResult
  • So that: I can deterministically await background task completion in tests and other sync-sensitive contexts

Acceptance Criteria

Scenario: Caller awaits background tasks after hook invocation
  Given a hook with one or more FIRE_AND_FORGET plugins registered
  When invoke_hook() returns a PluginResult
  Then result.background_tasks contains the asyncio.Task handles created for those plugins

Scenario: Callers that ignore background_tasks see no behavior change
  Given a hook invocation with no caller reading background_tasks
  When invoke_hook() completes
  Then fire-and-forget tasks still run in the background as before

Design Sketch (optional)

Add a background_tasks field to PluginResult:

class PluginResult(BaseModel, Generic[T]):
    ...
    background_tasks: list[asyncio.Task[Any]] = Field(default_factory=list)

Populate it in _fire_and_forget_tasks() by collecting the return values of
asyncio.create_task(). Callers that need a deterministic sync point can then:

result, payload = await manager.invoke_hook(...)
await asyncio.gather(*result.background_tasks, return_exceptions=True)

Alternatives Considered (optional)

  • Fixed asyncio.sleep() — current workaround; racy under load, arbitrary timeout.
  • asyncio.all_tasks() snapshot — capture tasks before/after invoke_hook(); fragile (captures unrelated tasks, depends on internal implementation detail).
  • Test-only asyncio.create_task patch — monkey-patching asyncio in tests; messy and global.

Additional Context

Surfaced while writing deterministic tests for telemetry plugins in a downstream
project that uses CPEX's FIRE_AND_FORGET mode. See:
generative-computing/mellea#691

Activity

  1. added
    enhancement
    on Apr 20, 2026
  2. changed the title [-][FEATURE]:[/-] [+][FEATURE]: Expose FIRE_AND_FORGET task handles on PluginResult for deterministic awaiting[/+] on Apr 20, 2026
  3. araujof commented on Apr 21, 2026

    @araujof
    Contributor

    Thanks for this feature request @ajbozarth ! Would you like to contribute a PR?

  4. added this to the 0.1.0 milestone on Apr 21, 2026
  5. ajbozarth commented on Apr 21, 2026

    @ajbozarth
    ContributorAuthor

    Sure I can contribute a PR

  6. moved this from Backlog to In progress in CPEXon Apr 21, 2026
  7. moved this from In progress to Done in CPEXon Apr 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancement

Type

No type

Projects

  • Status
    Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions