Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit c292200

Browse files
authored
feat: add stop function to allow shutting down the debug agent (#1147)
1 parent c5bbb4e commit c292200

File tree

14 files changed

+112
-6
lines changed

14 files changed

+112
-6
lines changed

src/agent/debuglet.ts

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ export interface FindFilesResult {
187187
export class Debuglet extends EventEmitter {
188188
private debug: Debug;
189189
private v8debug: DebugApi | null;
190+
private started: boolean;
190191
private running: boolean;
191192
private project: string | null;
192193
private controller: Controller | null;
@@ -241,6 +242,9 @@ export class Debuglet extends EventEmitter {
241242
*/
242243
this.v8debug = null;
243244

245+
/** @private {boolean} */
246+
this.started = false;
247+
244248
/** @private {boolean} */
245249
this.running = false;
246250

@@ -351,6 +355,7 @@ export class Debuglet extends EventEmitter {
351355
* @private
352356
*/
353357
async start(): Promise<void> {
358+
this.started = true;
354359
const stat = util.promisify(fs.stat);
355360

356361
try {
@@ -734,6 +739,10 @@ export class Debuglet extends EventEmitter {
734739
}
735740

736741
setTimeout(() => {
742+
if (!this.running) {
743+
this.logger.info('Debuglet is stopped; not registering');
744+
return;
745+
}
737746
assert(that.controller);
738747
if (!that.running) {
739748
onError(new Error('Debuglet not running'));
@@ -785,6 +794,10 @@ export class Debuglet extends EventEmitter {
785794
}
786795

787796
startListeningForBreakpoints_(): void {
797+
if (!this.running) {
798+
this.logger.info('Debuglet is stopped; not listening for breakpoints');
799+
return;
800+
}
788801
assert(this.controller);
789802
// TODO: Handle the case where this.debuggee is null or not properly registered.
790803
this.controller.subscribeToBreakpoints(
@@ -1072,16 +1085,30 @@ export class Debuglet extends EventEmitter {
10721085
}
10731086

10741087
/**
1075-
* Stops the Debuglet. This is for testing purposes only. Stop should only be
1076-
* called on a agent that has started (i.e. emitted the 'started' event).
1077-
* Calling this while the agent is initializing may not necessarily stop all
1078-
* pending operations.
1088+
* Stops the Debuglet.
1089+
*
1090+
* Stop should only be called on a agent that has started.
10791091
*/
10801092
stop(): void {
1093+
if (this.running) {
1094+
this.stopController();
1095+
} else {
1096+
if (!this.started) {
1097+
this.logger.info('Attempt to stop Debuglet before it was started');
1098+
return;
1099+
}
1100+
this.on('started', () => {
1101+
this.stopController();
1102+
});
1103+
}
1104+
}
1105+
1106+
stopController(): void {
10811107
assert(this.controller);
10821108
assert.ok(this.running, 'stop can only be called on a running agent');
10831109
this.logger.debug('Stopping Debuglet');
10841110
this.running = false;
1111+
this.started = false;
10851112
this.controller.stop();
10861113
this.emit('stopped');
10871114
}

src/agent/firebase-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ export class FirebaseController implements Controller {
349349
debuglog(`starting to mark every ${this.markActivePeriodMsec} ms`);
350350
this.markActiveInterval = setInterval(() => {
351351
this.markDebuggeeActive();
352-
}, this.markActivePeriodMsec);
352+
}, this.markActivePeriodMsec).unref();
353353
}
354354

355355
/**

src/index.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import * as util from 'util';
2323
const debuglog = util.debuglog('cdbg');
2424

2525
// Singleton.
26-
let debuglet: Debuglet;
26+
let debuglet: Debuglet | undefined;
2727

2828
/**
2929
* Start the Debug agent that will make your application available for debugging
@@ -86,3 +86,14 @@ function mergeConfigs<T>(options: T & {debug?: T}): T {
8686
export function get(): Debuglet | undefined {
8787
return debuglet;
8888
}
89+
90+
/**
91+
* Cleanly shut down the debug agent.
92+
*
93+
* This will free up all resources. It may be necessary to call this to allow
94+
* your process to shut down cleanly.
95+
*/
96+
export function stop(): void {
97+
debuglet?.stop();
98+
debuglet = undefined;
99+
}

system-test/fixtures/sample/src/allowExpressions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313

1414
import * as debug from '@google-cloud/debug-agent';
1515
debug.start({allowExpressions: true});
16+
debug.stop();

system-test/fixtures/sample/src/allowExpressionsJs.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
require('@google-cloud/debug-agent').start({
1515
allowExpressions: true,
1616
});
17+
require('@google-cloud/debug-agent').stop();

system-test/fixtures/sample/src/completeServiceContext.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ debug.start({
1919
version: 'Some version',
2020
},
2121
});
22+
debug.stop();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2023 Google LLC
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// https://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
import * as debug from '@google-cloud/debug-agent';
15+
debug.start({useFirebase: true});
16+
debug.stop();

system-test/fixtures/sample/src/import.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313

1414
import * as debug from '@google-cloud/debug-agent';
1515
debug.start();
16+
debug.stop();

system-test/fixtures/sample/src/noargs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313

1414
import * as debug from '@google-cloud/debug-agent';
1515
debug.start();
16+
debug.stop();

system-test/fixtures/sample/src/partialCapture.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ debug.start({
1717
maxFrames: 1,
1818
},
1919
});
20+
debug.stop();

0 commit comments

Comments
 (0)