Files
osmedeus/internal/functions/run_context_hooks.go
T
j3ssie 8448791c3c feat: propagate run cancellation to tmux sessions and remote SSH processes
- Track tmux sessions in ActiveRun and kill them on run cancel via new
  TmuxHooks indirection; expose killed_tmux_sessions in CLI and API responses
- Add ExecuteSSHCommand with remote pidfile + process-group kill watcher so
  cancelling a run actually terminates remote scans (not just the local session)
- Route ssh_exec/ssh_rsync/sync_* through the run's cancellable context via
  new RunContextHooks
- Switch docker-publish to sequential per-arch buildx builds + imagetools
  manifest to avoid OOM on multi-arch builds; add docker-buildx-setup target
- Cross-compile Dockerfile via BUILDPLATFORM/TARGETOS/TARGETARCH and retry
  SAST binary installs to survive QEMU-flaky downloads
- Bump version to v5.0.3
2026-05-26 00:06:26 +08:00

69 lines
1.9 KiB
Go

package functions
import (
"context"
"sync"
)
// RunContextHooks lets the executor expose the active run's cancellable
// context to helpers in this package without an import cycle. Helpers like
// ssh_exec / ssh_rsync / sync_* default to context.Background() otherwise,
// which means run cancellation never reaches them.
type RunContextHooks struct {
// Lookup returns the cancellable context tied to the given runUUID,
// or nil if no run is registered. Implementations must be safe for
// concurrent use.
Lookup func(runUUID string) context.Context
}
var (
runContextHooks *RunContextHooks
runContextHookMu sync.RWMutex
)
// RegisterRunContextHooks installs the run-context lookup. Called from the
// executor package at init.
func RegisterRunContextHooks(hooks *RunContextHooks) {
runContextHookMu.Lock()
defer runContextHookMu.Unlock()
runContextHooks = hooks
}
// UnregisterRunContextHooks clears the lookup (used in tests).
func UnregisterRunContextHooks() {
runContextHookMu.Lock()
defer runContextHookMu.Unlock()
runContextHooks = nil
}
// vmScanID returns the RunUUID associated with a vmFunc's current VM, or "".
// Centralised here so SSH helpers don't each reach into VMContext internals.
func vmScanID(vf *vmFunc) string {
if vf == nil {
return ""
}
if c := vf.getContext(); c != nil {
return c.scanID
}
return ""
}
// runContextFor returns the active run's context for runUUID, or
// context.Background() when no hook is registered or the run isn't tracked.
// Callers should derive their own deadlines from the result.
func runContextFor(runUUID string) context.Context {
if runUUID == "" {
return context.Background()
}
runContextHookMu.RLock()
h := runContextHooks
runContextHookMu.RUnlock()
if h == nil || h.Lookup == nil {
return context.Background()
}
if c := h.Lookup(runUUID); c != nil {
return c
}
return context.Background()
}