Skip to content

fix(security): resolve race condition and resource leaks in flow tracker#299

Open
tjsingleton wants to merge 2 commits intosmart-mcp-proxy:027-data-flow-securityfrom
tjsingleton:fix/spec-027-issues
Open

fix(security): resolve race condition and resource leaks in flow tracker#299
tjsingleton wants to merge 2 commits intosmart-mcp-proxy:027-data-flow-securityfrom
tjsingleton:fix/spec-027-issues

Conversation

@tjsingleton
Copy link
Contributor

Summary

Fixes 3 issues found during code review of #293 (Spec 027 data flow security):

  • Race condition: CheckFlow() writes to session fields under read lock — changed RLock to Lock
  • Goroutine leak: FlowService.Stop() never called in Runtime.Close() — added shutdown call
  • Missing callback: SetExpiryCallback never registered — flow session summaries now emit to activity log

Changes

Production fixes (2611a32):

  • internal/security/flow/tracker.go: RLockLock in CheckFlow (writes to LastActivity, ToolsUsed, Flows require write lock)
  • internal/runtime/runtime.go: Added flowService.Stop() in Close() before supervisor shutdown
  • internal/runtime/runtime.go: Registered SetExpiryCallback after Runtime construction to emit EventTypeActivityFlowSummary

Regression tests (ab95259):

  • TestFlowTracker_CheckFlow_ConcurrentRace: 15+ goroutines exercising concurrent CheckFlow + RecordOrigin
  • TestFlowService_Stop_Idempotent: triple-stop safety
  • TestFlowService_ExpiryCallback_EmitsSummary: verifies callback fires with correct FlowSummary

Test plan

  • go test -race ./internal/security/flow/... — all 34 tests pass
  • Race regression test confirms fix (would fail with RLock)
  • Code review verified: no deadlock risk, correct shutdown order, non-blocking callback

Stacked on #293

🤖 Generated with Claude Code

…ker (Spec 027)

- Change RLock→Lock in CheckFlow: method writes to session fields
  (LastActivity, ToolsUsed, Flows) which requires a write lock
- Add flowService.Stop() in Runtime.Close() to halt background
  goroutines (sessionExpiryLoop, cleanupLoop)
- Register SetExpiryCallback to emit activity flow summaries on
  session expiry, enabling the audit trail for data provenance
- TestFlowTracker_CheckFlow_ConcurrentRace: 15+ goroutines exercising
  concurrent CheckFlow+RecordOrigin to verify no data race
- TestFlowService_Stop_Idempotent: triple-stop safety verification
- TestFlowService_ExpiryCallback_EmitsSummary: verifies callback fires
  with correct FlowSummary on session expiry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant