Skip to content

Commit fb60618

Browse files
committed
nats backend services - remove sub on start to prevent potential subscription leak
1 parent 39d4867 commit fb60618

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

src/packages/jupyter/kernel/nats-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export async function initNatsService({
108108
return { buffer64: bufferToBase64(buffer) };
109109
},
110110
};
111-
return await createNatsJupyterService({
111+
return await createNatsJupyterService({
112112
project_id,
113113
path,
114114
impl,

src/packages/jupyter/redux/actions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ const MAX_BLOB_STORE_SIZE = 100 * 1000000;
2828

2929
declare const localStorage: any;
3030

31-
import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
3231
import * as immutable from "immutable";
3332
import { Actions } from "@cocalc/util/redux/Actions";
3433
import { three_way_merge } from "@cocalc/sync/editor/generic/util";
@@ -50,6 +49,7 @@ import type { Client } from "@cocalc/sync/client/types";
5049
import latexEnvs from "@cocalc/util/latex-envs";
5150
import { jupyterApiClient } from "@cocalc/nats/service/jupyter";
5251
import { type AKV, akv } from "@cocalc/nats/sync/akv";
52+
import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
5353

5454
const { close, required, defaults } = misc;
5555

src/packages/jupyter/redux/project-actions.ts

+10-4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import { removeJupyterRedux } from "@cocalc/jupyter/kernel";
3333
import { initNatsService } from "@cocalc/jupyter/kernel/nats-service";
3434
import { type DKV, dkv } from "@cocalc/nats/sync/dkv";
3535
import { computeServerManager } from "@cocalc/nats/compute/manager";
36+
import { reuseInFlight } from "@cocalc/util/reuse-in-flight";
3637

3738
// see https://github.com/sagemathinc/cocalc/issues/8060
3839
const MAX_OUTPUT_SAVE_DELAY = 30000;
@@ -216,15 +217,20 @@ export class JupyterActions extends JupyterActions0 {
216217
);
217218
}
218219

219-
private initNatsApi = async () => {
220-
const service = await initNatsService({
220+
private natsService?;
221+
private initNatsApi = reuseInFlight(async () => {
222+
if (this.natsService != null) {
223+
this.natsService.close();
224+
this.natsService = null;
225+
}
226+
const service = (this.natsService = await initNatsService({
221227
project_id: this.project_id,
222228
path: this.path,
223-
});
229+
}));
224230
this.syncdb.on("closed", () => {
225231
service.close();
226232
});
227-
};
233+
});
228234

229235
private _first_load = async () => {
230236
const dbg = this.dbg("_first_load");

src/packages/nats/service/service.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { requestMany, respondMany } from "./many";
2626
import { encodeBase64, waitUntilConnected } from "@cocalc/nats/util";
2727

2828
const DEFAULT_TIMEOUT = 10 * 1000;
29-
const MONITOR_INTERVAL = 90 * 1000;
29+
const MONITOR_INTERVAL = 45 * 1000;
3030

3131
// switching this is awkward since it would have to be changed in projects
3232
// and frontends or things would hang. I'm making it toggleable just for
@@ -272,8 +272,18 @@ export class NatsService extends EventEmitter {
272272
try {
273273
await waitUntilConnected();
274274
this.emit("starting");
275+
275276
this.log("starting service");
276277
const env = await getEnv();
278+
279+
// close any subscriptions by this client to the subject, which might be left from previous runs of this service.
280+
// @ts-ignore
281+
for (const sub of env.nc.protocol.subscriptions.subs) {
282+
if (sub[1].subject == this.subject) {
283+
sub[1].close();
284+
}
285+
}
286+
277287
if (this.options.enableServiceFramework ?? ENABLE_SERVICE_FRAMEWORK) {
278288
const svcm = new Svcm(env.nc);
279289
const service = await svcm.add({

src/packages/util/smc-version.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
/* autogenerated by the update_version script */
2-
exports.version=1745705764;
2+
exports.version=1745732279;

0 commit comments

Comments
 (0)