@@ -187,6 +187,7 @@ export interface FindFilesResult {
187187export 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 }
0 commit comments