Skip to content

Conversation

NathanFlurry
Copy link
Member

No description provided.

Copy link

vercel bot commented Sep 5, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Comments Updated (UTC)
rivet-site Canceled Canceled Sep 8, 2025 10:06pm
1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
rivet-studio Ignored Ignored Preview Sep 8, 2025 10:06pm

Copy link

claude bot commented Sep 5, 2025

Claude encountered an error —— View job


I'll analyze this and get back to you.

Copy link
Member Author

NathanFlurry commented Sep 5, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


How to use the Graphite Merge Queue

Add the label merge-queue to this PR to add it to the merge queue.

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

Copy link

pkg-pr-new bot commented Sep 5, 2025

Open in StackBlitz

npm i https://pkg.pr.new/rivet-gg/engine/@rivetkit/engine-runner@2872
npm i https://pkg.pr.new/rivet-gg/engine/@rivetkit/engine-runner-protocol@2872
npm i https://pkg.pr.new/rivet-gg/engine/@rivetkit/engine-tunnel-protocol@2872

commit: f452a88

Comment on lines +404 to +410
onDisconnected: () => {
if (!connected) {
// First connection attempt failed
reject(new Error("Tunnel connection failed"));
}
// If already connected, tunnel will handle reconnection automatically
},
Copy link
Contributor

Choose a reason for hiding this comment

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

There's a potential race condition in the tunnel connection handling. If the tunnel disconnects immediately after connecting, the Promise will have already resolved successfully, but the system would be in a disconnected state.

Consider enhancing the connection state management by either:

  1. Using a timeout for the initial connection attempt
  2. Adding a state variable that tracks whether the connection is stable
  3. Implementing a more robust reconnection mechanism that handles the case where disconnection happens shortly after initial connection

This would prevent the code from proceeding with a tunnel that appears connected but actually isn't available for network requests, which seems to be the original issue this PR is trying to solve.

Suggested change
onDisconnected: () => {
if (!connected) {
// First connection attempt failed
reject(new Error("Tunnel connection failed"));
}
// If already connected, tunnel will handle reconnection automatically
},
onDisconnected: () => {
if (!connected) {
// First connection attempt failed
reject(new Error("Tunnel connection failed"));
} else if (Date.now() - connectionEstablishedTime < 2000) {
// Connection was established but disconnected within 2 seconds
// This suggests an unstable connection that shouldn't be considered successful
reject(new Error("Tunnel connection unstable - disconnected shortly after connecting"));
}
// If already connected for a significant time, tunnel will handle reconnection automatically
},

Spotted by Diamond

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

Comment on lines +384 to +420
async #openTunnelAndWait(): Promise<void> {
return new Promise((resolve, reject) => {
const url = this.pegboardRelayUrl;
//console.log("[RUNNER] Opening tunnel to:", url);
//console.log("[RUNNER] Current runner ID:", this.runnerId || "none");
//console.log("[RUNNER] Active actors count:", this.#actors.size);

let connected = false;

this.#tunnel = new Tunnel(url);
this.#tunnel.setCallbacks({
fetch: this.#config.fetch,
websocket: this.#config.websocket,
onConnected: () => {
if (!connected) {
connected = true;
//console.log("[RUNNER] Tunnel connected");
resolve();
}
},
onDisconnected: () => {
if (!connected) {
// First connection attempt failed
reject(new Error("Tunnel connection failed"));
}
// If already connected, tunnel will handle reconnection automatically
},
});
this.#tunnel.start();

// Re-register all active actors with the new tunnel
for (const actorId of this.#actors.keys()) {
//console.log("[RUNNER] Re-registering actor with tunnel:", actorId);
this.#tunnel.registerActor(actorId);
}
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The current implementation has a potential resource leak when the tunnel connection fails. If the Promise is rejected, the tunnel instance and its callbacks remain active without proper cleanup.

Consider adding cleanup code in the rejection path:

onDisconnected: () => {
  if (!connected) {
    // First connection attempt failed
    this.#tunnel.shutdown();
    this.#tunnel = null;
    reject(new Error("Tunnel connection failed"));
  }
  // If already connected, tunnel will handle reconnection automatically
}

This ensures the tunnel is properly shut down when the connection fails, preventing resource leaks and avoiding callback execution on a failed connection.

Suggested change
async #openTunnelAndWait(): Promise<void> {
return new Promise((resolve, reject) => {
const url = this.pegboardRelayUrl;
//console.log("[RUNNER] Opening tunnel to:", url);
//console.log("[RUNNER] Current runner ID:", this.runnerId || "none");
//console.log("[RUNNER] Active actors count:", this.#actors.size);
let connected = false;
this.#tunnel = new Tunnel(url);
this.#tunnel.setCallbacks({
fetch: this.#config.fetch,
websocket: this.#config.websocket,
onConnected: () => {
if (!connected) {
connected = true;
//console.log("[RUNNER] Tunnel connected");
resolve();
}
},
onDisconnected: () => {
if (!connected) {
// First connection attempt failed
reject(new Error("Tunnel connection failed"));
}
// If already connected, tunnel will handle reconnection automatically
},
});
this.#tunnel.start();
// Re-register all active actors with the new tunnel
for (const actorId of this.#actors.keys()) {
//console.log("[RUNNER] Re-registering actor with tunnel:", actorId);
this.#tunnel.registerActor(actorId);
}
});
}
async #openTunnelAndWait(): Promise<void> {
return new Promise((resolve, reject) => {
const url = this.pegboardRelayUrl;
//console.log("[RUNNER] Opening tunnel to:", url);
//console.log("[RUNNER] Current runner ID:", this.runnerId || "none");
//console.log("[RUNNER] Active actors count:", this.#actors.size);
let connected = false;
this.#tunnel = new Tunnel(url);
this.#tunnel.setCallbacks({
fetch: this.#config.fetch,
websocket: this.#config.websocket,
onConnected: () => {
if (!connected) {
connected = true;
//console.log("[RUNNER] Tunnel connected");
resolve();
}
},
onDisconnected: () => {
if (!connected) {
// First connection attempt failed
this.#tunnel.shutdown();
this.#tunnel = null;
reject(new Error("Tunnel connection failed"));
}
// If already connected, tunnel will handle reconnection automatically
},
});
this.#tunnel.start();
// Re-register all active actors with the new tunnel
for (const actorId of this.#actors.keys()) {
//console.log("[RUNNER] Re-registering actor with tunnel:", actorId);
this.#tunnel.registerActor(actorId);
}
});
}

Spotted by Diamond

Fix in Graphite


Is this helpful? React 👍 or 👎 to let us know.

Copy link

claude bot commented Sep 8, 2025

Claude encountered an error —— View job


I'll analyze this and get back to you.

Copy link

claude bot commented Sep 8, 2025

Claude encountered an error —— View job


I'll analyze this and get back to you.

@graphite-app graphite-app bot changed the base branch from 09-04-chore_ups_remove_no_responders to graphite-base/2872 September 8, 2025 21:51
@graphite-app graphite-app bot force-pushed the graphite-base/2872 branch from 0678b46 to 6b4c00b Compare September 8, 2025 21:52
@graphite-app graphite-app bot force-pushed the 09-04-chore_runner_connect_tunnel_before_connecting_pb_ws branch from 90ce950 to c140b96 Compare September 8, 2025 21:52
@graphite-app graphite-app bot changed the base branch from graphite-base/2872 to main September 8, 2025 21:53
Copy link
Contributor

graphite-app bot commented Sep 9, 2025

Merge activity

  • Sep 9, 5:46 PM UTC: NathanFlurry added this pull request to the Graphite merge queue.
  • Sep 9, 5:47 PM UTC: CI is running for this pull request on a draft pull request (#2896) due to your merge queue CI optimization settings.
  • Sep 9, 5:49 PM UTC: Merged by the Graphite merge queue via draft PR: #2896.

@graphite-app graphite-app bot closed this Sep 9, 2025
@graphite-app graphite-app bot deleted the 09-04-chore_runner_connect_tunnel_before_connecting_pb_ws branch September 9, 2025 17:49
This was referenced Sep 15, 2025
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.

2 participants