Skip to content

fix(server): keep each sandbox's logs on its parent gateway - #6153

Open
FrostGod wants to merge 2 commits into
mainfrom
fix/5462-sandbox-log-home/FrostGod
Open

FrostGod wants to merge 2 commits into
mainfrom
fix/5462-sandbox-log-home/FrostGod

Conversation

@FrostGod

@FrostGod FrostGod commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

With several gateway replicas, each kept its own sandbox log buffer, so openshell logs showed different lines depending on which replica answered. Now each sandbox's logs live on one replica, its parent: the one holding the supervisor session.

Related Issue

Closes #5462

Changes

  • The supervisor pushes logs to the gateway that accepted its session and reconnects when that changes, in both dedicated and shared mode.
  • Other gateways forward the lines and events they produce for a sandbox to its parent (PeerPublishSandboxEvents), in batches of at most 256 KiB. Home lookups run in the background, so a slow one holds back only that sandbox.
  • GetSandboxLogs and log/event WatchSandbox are proxied to the parent (PeerGetSandboxLogs, PeerWatchSandbox), falling back to the local buffer on error.
  • The parent is the session holder, else the last recorded home, else the replica the session is expected to land on.
  • When another live replica records itself as the home, the old one drops its buffer within about 40 seconds (one batched read every 30 seconds), which ends its open tails. History is not carried over.
  • New metric openshell_server_sandbox_log_events_dropped_total{reason}.
  • Single-replica (SQLite) gateways are unchanged.
  • Docs: accessing-logs.mdx and gateway-metrics.mdx.

Testing

  • cargo test and clippy -D warnings for openshell-server, openshell-supervisor, openshell-supervisor-process, openshell-sdk
  • Unit tests for the bus router, batch sizing, pooled replica mapping, and the move sweep
  • Manual E2E on k3s with two gateways and Postgres, dedicated and shared supervisors: logs and --tail matched from both gateways (35/13 split before). After deleting the parent gateway or the hosting supervisor, the session moved, open tails ended, and logs matched again. No events dropped.
  • E2E tests added/updated

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)

In multi-gateway deployments every replica kept its own log buffer, so
openshell logs returned different lines depending on which replica served
the request.

The supervisor now pushes logs to the gateway that accepted its session,
and gateways forward gateway lines and platform events to that parent.
GetSandboxLogs and log/event WatchSandbox are proxied to the parent.
When the parent changes, the old replica drops its buffer, which ends
open watch streams.

Closes #5462

Signed-off-by: divesh <dgude@nvidia.com>
@github-actions github-actions Bot added the state:accepted A maintainer decided OpenShell should pursue this issue label Dec 2, 2026
@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown

@EmilienM EmilienM left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another iteration needed: 5 major (inline)

The parent-resolution design holds up; the problems are in the new log_routing.rs mechanics. The two that decide it for me: lines are deterministically lost around home moves (#5462 asks for the opposite), and the router couples every sandbox's log path to serial store reads with silent drops behind a 16k queue.

How this was reviewed
  • Commit 30b6c449b, round 1
  • Deep review, because the change crosses the gateway peer/auth boundary (auth/method_authz.rs, auth/peer.rs) and adds new peer RPCs to the multi-replica routing path
  • Passes: architecture, performance, security, scalability, rust, kubernetes, correctness, a defensive review
  • Independent skeptics checked each candidate finding (3 per code-lens finding, 2 for architecture and scalability, 1 per minor); 11 of 12 held up
  • Repo rules read from main: AGENTS.md, CLAUDE.md, CONTRIBUTING.md, docs/CONTRIBUTING.mdx
  • Not covered: the multi-replica E2E lanes have not run on this head yet (CI approval pending), so the manual k3s verification in the description is the only end-to-end evidence

let home = if let Some(home) = homes.get(&sandbox_id) {
home.clone()
} else {
let home = log_home_peer(&state, &sandbox_id).await;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major: route_events is the single consumer of ROUTER_QUEUE, and on a cache miss it awaits log_home_peer inline, so one sandbox's home resolution (a store read, re-paid for every remote-home sandbox after the homes.clear() every 2s) stalls routing for every sandbox on this replica. Once the 16k queue fills, route() in tracing_bus.rs drops events with no signal, losing lines that main always buffered. Move home resolution off the event path (a background-refreshed cache, or per-sandbox ordering tasks) and make the drop observable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Home lookups now run in a background task per sandbox, so a slow read only holds back that sandbox (its events wait in order). It also checks the local session before trusting the cache, and drops show up in openshell_server_sandbox_log_events_dropped_total. Fixed in 80311a0.

}
}
_ = timer.tick() => {
for sandbox_id in state.tracing_log_bus.idle_sandbox_ids(Duration::ZERO) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major: drop_moved_buffers scans every buffered sandbox every 5s and does awaited, unbatched store reads for each one without a local session (owner record, then sandbox_log_home), forever for disconnected supervisors and retained buffers. bus_sweeper already solves this job shape the intended way: batched primary-key lookups, a 30s interval, and a 2-minute idle gate.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, switched it to the bus_sweeper shape: every 30s, only idle buffers with no local session, one batched get_many on the home records. It drops a buffer only when another live replica has recorded itself as the home.

_ = timer.tick() => {
for sandbox_id in state.tracing_log_bus.idle_sandbox_ids(Duration::ZERO) {
if log_home_peer(&state, &sandbox_id).await.is_some() {
state.tracing_log_bus.remove(&sandbox_id);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major: Lines are lost on every home move. While a session is in flight the old home still resolves itself (fresh owner record or its own last_home) and buffers the supervisor's push locally; once the record flips, drop_moved_buffers deletes that buffer instead of forwarding it. The 2s homes cache also misroutes on both sides of a move. That breaks #5462's "without losing lines" criterion and the claim in the log_push.rs comment. Forward the tail to the new home before remove(), and re-check has_session before trusting a cached entry.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is on purpose. A move only happens when the parent changes, and we already lose the in-memory buffer when a gateway crashes, so handing the old buffer over felt like more machinery than this fix needs. The new home starts fresh and open tails end. I updated #5462 to say that.

/// Events waiting to be sent to one peer; more are dropped.
const PEER_QUEUE: usize = 8 * 1024;
/// Most events per forwarded batch.
const MAX_BATCH: usize = 512;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major: Batches are capped at 512 events but not by size, and the gateway decodes at most 1 MiB (MAX_GRPC_DECODE_SIZE in multiplex.rs). A sandbox with KiB-scale lines (JSON, stack traces) makes every full batch fail the RPC; the batch is then dropped at debug level and the healthy peer channel is evicted. The supervisor push path sizes its batches for this on purpose (LOG_PUSH_BATCH_LINES = 50, "below the gateway's ingest cap"). Budget by encoded_len() (flushing around 256 KiB), and don't evict the channel on ResourceExhausted.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, that was a real bug. Batches are now capped at 256 KiB encoded as well as 512 events, and a ResourceExhausted/OutOfRange reply no longer evicts the channel. Failed batches get counted too.

"",
state.replica_id.as_bytes(),
None,
WriteCondition::Unconditional,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Major: record_home does a read-then-unconditional-write, so if another replica accepts a reconnect between the GET and the PUT, this write clobbers its record. Once the real owner's record expires, resolution falls back to the stale home, and that replica's sweep then deletes the current history within 5s. The store already has WriteCondition::MatchResourceVersion; condition the write on the version the GET returns.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. The write now uses MatchResourceVersion (or MustCreate) from the read, and checks we still hold the session first. A conflict means a newer home won, so it just backs off.

Resolve log homes in background tasks so a slow store read holds back
only that sandbox's events, and count dropped events in
openshell_server_sandbox_log_events_dropped_total.

Check for moved log homes every 30 seconds with one batched read of the
log home records, and drop a buffer only when another live replica
recorded itself as the home. Record the home with a version-conditioned
write.

Cap forwarded batches at 256 KiB and keep the peer channel when a batch
is rejected for size.

Signed-off-by: divesh <dgude@nvidia.com>
@FrostGod

FrostGod commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test 80311a0

This branch has not been deployed

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

Labels

state:accepted A maintainer decided OpenShell should pursue this issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: sandbox logs and watch streams ignore gateway ownership in multi-replica mode

2 participants