Repository navigation
feat(interactive): send the controlling window's jdv address to R when control changes #2015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,7 +57,13 @@ export class SessionAgent { | |
| private journal: SessionJournal; | ||
| private assets: AssetStore; | ||
| private clients = new Set<Client>(); | ||
| private lease?: { client: string; until: number }; | ||
| private lease?: { | ||
| client: string; | ||
| until: number; | ||
| jdv?: { socket: net.Socket; address: string }; | ||
| }; | ||
| private leaseRevision = 0; | ||
| private leaseTimer?: NodeJS.Timeout; | ||
| private manifest: SessionManifest; | ||
| private backend: SessionBackend; | ||
| private unsubscribe: () => void; | ||
|
|
@@ -387,20 +393,48 @@ export class SessionAgent { | |
| socket.on('close', () => { | ||
| clearTimeout(authTimer); | ||
| this.clients.delete(client); | ||
| if (this.lease?.jdv?.socket === socket) { | ||
| this.lease.jdv = undefined; | ||
| this.publishLease(); | ||
| } | ||
| if (this.manifest.status === 'exited' && !this.clients.size && !this.stopped) { | ||
| this.closeAfterExit(true); | ||
| } | ||
| // Keep the lease briefly for reconnect; accepted executions keep running. | ||
| }); | ||
| } | ||
|
|
||
| private publishLease(lostClient?: string): void { | ||
| const revision = ++this.leaseRevision; | ||
| const jdv = this.lease?.jdv; | ||
| this.backend.setJdvRoute?.(revision, jdv?.address); | ||
| if (lostClient !== undefined) { | ||
| for (const client of this.clients) { | ||
| if (client.authenticated && client.id === lostClient && !client.socket.destroyed) { | ||
| this.send(client.socket, { control: false, leaseRevision: revision }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private renewLease(): void { | ||
| const lease = this.lease!; | ||
| lease.until = Date.now() + 60000; | ||
| clearTimeout(this.leaseTimer); | ||
| this.leaseTimer = setTimeout(() => { | ||
| this.lease = undefined; | ||
| this.publishLease(lease.client); | ||
| }, 60000); | ||
| this.leaseTimer.unref(); | ||
| } | ||
|
|
||
| private requireControl(client: Client): void { | ||
| if (!this.lease || this.lease.client !== client.id || this.lease.until < Date.now()) { | ||
| throw new Error( | ||
| 'This window is observing the session. Take control before changing it.', | ||
| ); | ||
| } | ||
| this.lease.until = Date.now() + 60000; | ||
| this.renewLease(); | ||
| } | ||
|
|
||
| private async handleClient(client: Client, request: Record<string, unknown>): Promise<void> { | ||
|
|
@@ -432,32 +466,56 @@ export class SessionAgent { | |
| case 'messagePage': | ||
| result = client.pages.read(String(params.token), Number(params.offset)); | ||
| break; | ||
| case 'claim': | ||
| case 'claim': { | ||
| const previous = this.lease; | ||
| if ( | ||
| !this.lease || | ||
| this.lease.until < Date.now() || | ||
| this.lease.client === client.id || | ||
| previous?.client === client.id || | ||
| params.force === true || | ||
| ![...this.clients].some( | ||
| (other) => | ||
| other.authenticated && | ||
| other.id === this.lease?.client && | ||
| other.id === previous?.client && | ||
| !other.socket.destroyed, | ||
| ) | ||
| ) { | ||
| this.lease = { client: client.id, until: Date.now() + 60000 }; | ||
| result = true; | ||
| const address = params.jdvAddress; | ||
| if (address !== undefined && (typeof address !== 'string' || !address.trim())) { | ||
| throw new Error('Invalid jdv listener address'); | ||
| } | ||
| const sameWindow = previous?.client === client.id; | ||
| this.lease = { | ||
| client: client.id, | ||
| until: 0, | ||
| // Helper connections can renew control without replacing the listener. | ||
| jdv: | ||
| address !== undefined | ||
| ? { socket: client.socket, address } | ||
| : sameWindow | ||
| ? previous.jdv | ||
| : undefined, | ||
| }; | ||
| this.renewLease(); | ||
| if (!sameWindow || this.lease.jdv !== previous?.jdv) { | ||
| this.publishLease(sameWindow ? undefined : previous?.client); | ||
| } | ||
| } else { | ||
| result = false; | ||
| } | ||
| if (params.leaseRevision === true) { | ||
| result = { accepted: result, leaseRevision: this.leaseRevision }; | ||
| } | ||
| break; | ||
| } | ||
| case 'heartbeat': | ||
| if (this.lease?.client === client.id) { | ||
| this.lease.until = Date.now() + 60000; | ||
| if (this.lease?.client === client.id && this.lease.until >= Date.now()) { | ||
| this.renewLease(); | ||
| } | ||
| result = { | ||
| status: this.manifest.status, | ||
| control: this.lease?.client === client.id, | ||
| control: this.lease?.client === client.id && this.lease.until >= Date.now(), | ||
| leaseRevision: this.leaseRevision, | ||
| queued: this.queue.length, | ||
| }; | ||
| break; | ||
|
|
@@ -655,9 +713,13 @@ export class SessionAgent { | |
| } | ||
| throw new Error('R is busy; cached data remains available. Retry when idle.'); | ||
| } | ||
| const inspectionParams = object(params.params ?? {}); | ||
| if (method === 'jdv_open_viewer') { | ||
| delete inspectionParams.address; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honor explicit viewer opens from a protocol-1 client connected to the new agent. An older window can successfully claim this session without a |
||
| } | ||
| result = await this.backend.inspect({ | ||
| method, | ||
| params: object(params.params ?? {}), | ||
| params: inspectionParams, | ||
| timeout: 5000, | ||
| }); | ||
| break; | ||
|
|
@@ -718,6 +780,8 @@ export class SessionAgent { | |
| client.subscribed = false; | ||
| if (this.lease?.client === client.id) { | ||
| this.lease = undefined; | ||
| clearTimeout(this.leaseTimer); | ||
| this.publishLease(client.id); | ||
| } | ||
| break; | ||
| case 'stop': | ||
|
|
@@ -1091,7 +1155,9 @@ export class SessionAgent { | |
| this.dispatching = true; | ||
| const abort = new AbortController(); | ||
| this.dispatchAbort = abort; | ||
| const attempt = this.backend.dispatch(record, { signal: abort.signal }); | ||
| const attempt = this.backend.dispatch(record, { | ||
| signal: abort.signal, | ||
| }); | ||
| let settled!: () => void; | ||
| this.dispatchAttempt = new Promise((resolve) => { | ||
| settled = resolve; | ||
|
|
@@ -1268,6 +1334,7 @@ export class SessionAgent { | |
| this.workspaceRefresh.dispose(); | ||
| this.unsubscribe(); | ||
| clearTimeout(this.externalTimer); | ||
| clearTimeout(this.leaseTimer); | ||
| clearTimeout(this.redispatchTimer); | ||
| clearTimeout(this.handleCloseTimer); | ||
| this.assets.close(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Provide the controller’s address for
View()outside a dispatched cell. The bridge acknowledges withjdv = NULL, andinteractive_execute()restores that value on exit. I compared two entry points against4f5ac17. Opening a nested table from the Interactive Workspace tree now emits a legacydataviewnotification. Callingutils::View(data.frame(a = 1:3))at the adopted arf console opens no panel. The transcript drops the fallback display because it has no execution ID. Both opened jdv on the base commit. Please make the current controller’s address available between cells as well as preserving the individual cell’s address while it runs.