eclipse-cdt-cloud / cdt-gdb-adapter

CDT GDB Debug Adapter
Eclipse Public License 2.0
30 stars 40 forks source link

`isRunning` unreliable #325

Open colin-grant-work opened 6 months ago

colin-grant-work commented 6 months ago

The GDBDebugSession maintains a state isRunning and guards various actions depending on that value. However, a synchronous check of that value isn't guaranteed to accurately reflect GDB's state by the time it tries to process an MI command: some other part of the adapter code could have issued a continue command without GDB having yet emitted the event indicating that it has continued.

I'd suggest that we implement something like a tryIfNotRunning method along the following lines:

    protected async tryIfNotRunning<T, U>(
        action: () => Promise<T>,
        defaultValue: U
    ): Promise<T | U> {
        if (this.isRunning) {
            return defaultValue;
        }
        try {
            return await action();
        } catch (err) {
            if (
                err instanceof Error &&
                err.message.includes('target is running')
            ) {
                return defaultValue;
            }
            throw err;
        }
    }

That would centralize the handling of the error that such and such a command can't be handled while the target is running. I bet we could clean up the types a bit, too, for ergonomics.