Skip to content
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

Flush kernel logs to channel on exit #708

Merged
merged 1 commit into from
Jun 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions extensions/jupyter-adapter/src/JupyterKernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as positron from 'positron';
import * as zmq from 'zeromq/v5-compat';
import * as os from 'os';
import * as fs from 'fs';
import * as rl from 'readline';
import { JupyterSocket } from './JupyterSocket';
import { serializeJupyterMessage } from './JupyterMessageSerializer';
import { deserializeJupyterMessage } from './JupyterMessageDeserializer';
Expand Down Expand Up @@ -43,6 +44,7 @@ export class JupyterKernel extends EventEmitter implements vscode.Disposable {

/** An object that watches (tails) the kernel's log file */
private _logTail?: Tail;
private _logNLines: number;

/** The kernel's current state */
private _status: positron.RuntimeState;
Expand Down Expand Up @@ -110,6 +112,7 @@ export class JupyterKernel extends EventEmitter implements vscode.Disposable {
this._nextHeartbeat = null;
this._lastHeartbeat = 0;
this._exitCode = 0;
this._logNLines = 0;

// Set the initial status to uninitialized (we'll change it later if we
// discover it's actually running)
Expand Down Expand Up @@ -810,12 +813,9 @@ export class JupyterKernel extends EventEmitter implements vscode.Disposable {
* Dispose the kernel connection. Note that this does not dispose the
* session or the kernel itself; it remains running in a terminal.
*/
public dispose() {

public async dispose() {
// Clean up file watcher for log file
if (this._logTail) {
this._logTail.unwatch();
}
await this.disposeLogTail();

// Dispose heartbeat timers
this.disposeHeartbeatTimers();
Expand All @@ -824,6 +824,36 @@ export class JupyterKernel extends EventEmitter implements vscode.Disposable {
this.disposeAllSockets();
}

async disposeLogTail() {
if (!this._logTail) {
return;
}

this._logTail.unwatch();

// Push remaining lines in case new line events
// haven't had time to fire up before unwatching.
const file = this._session!.state.logFile;
if (!file || !fs.existsSync(file) || !this._logChannel) {
return;
}

const lines = rl.createInterface({
input: fs.createReadStream(file)
});

let i = 0;

for await (const line of lines) {
// Skip lines that we've already seen
if (++i <= this._logNLines) {
continue;
}
const prefix = this._spec.language;
this._logChannel.appendLine(`[${prefix}] ${line}`);
}
}

/**
* Dispose all sockets
*/
Expand Down Expand Up @@ -1046,8 +1076,14 @@ export class JupyterKernel extends EventEmitter implements vscode.Disposable {
return;
}

let ses = this;

// Establish a listener for new lines in the log file
this._logTail.on('line', function (data: string) {
// Keep track of how many lines we sent to the
// channel to properly emit the rest of the
// lines on disposal
ses._logNLines += 1;
output.appendLine(`[${prefix}] ${data}`);
});
this._logTail.on('error', function (error: string) {
Expand Down