Skip to content

DeleteEnvironment's session-count guard is check-then-act, so a raced CreateSession survives delete with a dangling, capability-less session #80

Description

@AmirF194

EnvironmentService.DeleteEnvironment checks for referencing sessions and deletes the environment in two separate calls: CountSessionsByEnvironment, then (inside uow.Run) environments.DeleteEnvironment. Nothing locks across the gap between them.

A CreateSession against that environment can land in the gap: the count already read 0, so the delete proceeds unconditionally and the environment is gone while a session still references it.

Sessions never persist the capability Requirements they were created with, they're re-derived from the live Environment at each placement pass (controlapp/scheduler.go, queuedEnvironment/candidatesFor). Once the environment resolves to nil, that session's capability filtering and setup/init hooks silently drop rather than erroring.

Confirmed in both store backends:

  • pgstore: DeleteEnvironment issues a plain DELETE with no NOT EXISTS subquery tying it to the count.
  • memstore: CountSessionsByEnvironment and DeleteEnvironment each take and release m.mu separately.

TestDeleteEnvironmentGuard only exercises the sequential case (session already present when the count runs), so nothing currently pins the race.

I have a fix (fold the check into one atomic repository call) with a regression test that reproduces the race through the real HTTP handler. PR to follow.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions