Repository navigation
Description
Currently, both Visual Studio and Visual Studio Code both support multiple concurrently running debug sessions, and Visual Studio Code supports hierarchical debug sessions which can represent debugging child processes or multiple "global objects" (example).
In this vein, there are some scenarios where it would be useful to be able to have multiple debug sessions running through a single debug adapter, which would allow for some more advanced orchestration between debug targets.
A particularly interesting example of this is when debugging a web page which has associated web workers and service workers. The Chrome DevTools protocol allows for the concept of a "flat session" debug mode, which allows a debugger to simultaneously debug all related pages and workers at the same time through a single connection using a "targetId" to route messages to the correct debug target. Additionally, it looks like this will soon become the default in Chrome DevTools, and the "non-flat" session mode will be deprecated (see crbug.com/991325).
In the above example, debugging a web page through a single "flat session" confers many benefits (e.g. being able to start each child target in a suspended state to allow the debugger to configure breakpoints and other set-up before the child starts running). However, running in "flat session" mode requires debugging these multiple targets through a single connection, something which, today, is hard to do using built-in functionality in the debug adapter protocol (it can be achieved by running in-process in VS Code and leveraging the DebugAdapterDescriptorFactory to share state between sessions, but that solution is clunky, and specific to the VS Code API, and therefor does not translate well to Visual Studio, for example).
Some other examples of scenarios where a "flat session" mode might be useful are:
- Running Node VMs as discrete child "processes"
- Lua VMs (as requested here: how can i debug multi "process" on one system process vscode-debugadapter-node#141)
- Debugging a Node back-end (e.g. Express) app simultaneously with the front-end browser page, and enabling "step-into" between both.
I've created a PR which, as a first step, adds an optional "sessionId" parameter to each protocol message. If this sounds reasonable, I think it would also be nice to give adapters a way to notify the host that a new child session has been attached, as right now an adapter has to manually call VSCode or Visual Studio APIs to launch a new debug session and then trap it on entry to achieve this.
Let me know what you think!
Activity
roblourens commentedon Oct 22, 2019
I like this. Does the protocol need to think about how a client knows whether to route a new request to the same adapter vs creating a new one, or is that up to the client?
EricCornelson commentedon Oct 22, 2019
To start with, I think we could leave it up to the client. On the VS Code side, it's pretty easy to leverage the DebugAdapterDescriptorFactory to trap new launches and re-use the same adapter instance. On VS that's a little harder, since there's no similar built in "factory" interface, but since adapters using the PineZorro extension have the ability to handle launching themselves, it's fairly easy to just use that to route everything back through the same adapter instance (which is what we're doing right now).
If we end up finding this model useful outside of javascript debugging, it would be nice to have a way to tell the host that we want to re-use the same adapter instance in "flat session". For that we could just use the capabilities event and publish that an adapter can support flat session mode.
connor4312 commentedon Oct 23, 2019
This sounds neat. I like the intention and it'd certainly make some scenarios easier as you mentioned. Stepping between the client and service is super interesting, being able to step through
fetch()into an Express route and back out again would be neat. In the (longer term) future it might be useful to have a contribution model in vscode-pwa to give intelligence around frameworks like that, beyond the generic logic we have out of the box...In implementation we may want to have a new preflight request prior to launching, for the client to confirm whether it is able to attach a session to the existing adapter. Adapters won't all support flat sessions for some time, and then there might(?) be scenarios where one adapter is unable to flatten requested sessions into its existing instance. A graceful rejection or a 'method not found' error would cause a new instance of the adapter to start.
weinand commentedon Oct 24, 2019
Eric Cornelson (@EricCornelson) Thanks for this interesting proposal.
My first reaction:
This feature sounds like an optimisation for functionality that already exists.
Stepping between client and server code is daily business for VS Code and it is supported for all combinations of client and server languages (e.g. TS in browser and Java and Python in the backend servers).
With this "optimisation" a single debug adapter can be used to handle multiple sessions if these sessions use the same runtime. This makes sense for a CDP-DA but I'm not aware of many other runtimes that would want to support this.
On the other hand I see quite some VS Code restructuring work necessary to support this feature.
So before we want to commit to this effort, you must convince me that there are no alternatives. ;-)
One alternative is what the Java extension is currently doing: they are running a single DA process that serves multiple debug sessions through different socket connections. In the DA server all sessions are using the same underlying runtime and/or debug target connection.
So this approach already "Allows multiple debug sessions through a single adapter". But instead of multiplexing via a new "sessionId" attribute, communication channels of the OS are used.
Would this solve the issue you are trying to address?
int19h commentedon Oct 24, 2019
The Python adapter is using this model - a single adapter managing several related concurrent debug sessions, with multiple connections to it from VSCode via sockets - for multiprocessing scenarios (although we are still working on that implementation). Having a single channel to talk to the IDE would be marginally simpler on the adapter side, but it's not really a big deal.
One related thing that would be nice to have is some way for a session to tell the client to create another session (i.e. effectively, issue a reverse "attach" request, that would result in creation of a new session with the supplied debug configuration). We're using a custom event for this, and it works... but it's not a client-agnostic solution, and requires re-implementing the client part for every IDE. Since debugging multiple related processes is a generic scenario, not really tied to any particular language or IDE, and it needs this bit for the adapter to orchestrate things on the client, it would be nice to have that supported in the core DAP protocol.
EricCornelson commentedon Oct 25, 2019
Andre Weinand (@weinand) We actually started our proof of concept using sockets, but the big draw for running everything through a single communication channel is remote debugging in VS. Multiple socket connections are fine for local debugging, and probably even debugging a remote workspace in VS Code, but if the user is debugging a remote process either in a VM or a server, and their workspace is local, it gets trickier.
We currently have a way through VSDebugAdapterHost and msvsmon to launch the debug adapter process on the remote machine, where the adapter can then launch the desired program and start debugging, and we communicate with the adapter process via its stdin/stdout through an SSH tunnel. But we can't open a socket connection to a debug server process on the remote machine without either manually doing some port-forwarding manually or dealing with firewall issues (that is, there's nothing built in to VS that would make this easy for us, as far as I know).
Andrew Crawley (@andrewcrawley) pointed out that if we wanted to go the debug server route, we could do something like launch the first adapter as a debug server, and then for each child debug session in VS launch a "proxy" process that would make the connection to the debug server and then pipe the traffic back through its stdin/stdout. While that could theoretically work, we both agreed that this was a less-than-ideal workaround, as it involves a lot more moving parts, and has more potential for problems to arise.
I'm not sure how that particular scenario translates to other adapter projects, but it was the biggest factor for us to go with the "flat session" communication model.
Pavel Minaev (@int19h) +1 We are doing the same thing RE: the custom "reverse attach" event on the VS side, and have had the same thoughts. I didn't add that piece to the proposal, but maybe that's a better place to start. This is where we are passing the sessionId for each new child session back to the host as well, and my thought is that we're essentially telling the host "we are already managing this child debug session, just letting you know".
I'm curious, though, if we added a "reverse attach" event to the protocol, how would hosts be expected to handle that event? Right now, what we're doing with vscode-pwa is we manually send a new "start debugging" command and trap the launch to route it through the already-running adapter. But if we consider that "flat session" is not a thing in the protocol, what should the host do if it gets one of these events? We are essentially notifying the host so it can display the session in the UI and start sending commands to it. So then the question becomes, well how does the host start sending commands to this new session? We could give it a port to connect to, but I'd assume we wouldn't want to force a specific implementation (sockets) for new child sessions, and instead leave that up to the adapter implementation. Just firing a new "start debugging" command doesn't seem to make much sense to me either, because the session is actually already running in the current adapter, right, we just want to connect to it? If we fire a new "start debugging" command, then we are still in the business of having to trap the launch and route it through our existing adapter, either in a
DebugAdapterDescriptorFactoryfor VS Code, or some other IDE specific piece, or we are again forced in our implementation to use sockets (which is not always desirable, as in the scenario I mentioned above).So that leads me back to having a "flat session" option, where you can tell the host, "hey, I've connected to this new child session, please represent it for me in the UI and start sending messages to it over the existing connection with this identifier". 😃
I definitely understand that it doesn't make sense to add something to the protocol if it only fits a very narrow use case (especially if there is a workaround), which is also the main reason I drafted up the proposal. I was curious to see if there was any other adapters out there that might benefit from a "flat session" mode, and it sounds like there is at least one other!
int19h commentedon Oct 25, 2019
What we do right now is have the adapter send the complete debug configuration in the reverse attach request. The custom event handler in VSCode then starts a new debug session with that configuration. The adapter creates it by remembering the config it was passed in the request for the initial session, and then just changing it to "attach" (if it wasn't already), and adding a property to designate which known debuggee to attach to (by PID in our case).
On the VSCode side, we use
DebugAdapterDescriptorFactoryto make connections go directly to the socket, without spawning another adapter. If the primary connection was "launch", we assume that the IDE is running on the same machine, and thus those secondary connections can always use 127.0.0.1 as hostname. And for the port, we start a listener in the ephemeral port range, and then retrieve the actual port, and pass it in the reverse attach request. If it was "attach", then the original debug config already has the hostname and the port as properties, and they just get reused as is.For remote scenarios, this means that the other machine only needs to have that one port opened. This can then be tunneled via SSH for security purposes - for now, we tell the users to do this manually, but it would certainly be nice to have SSH as a built-in DAP transport directly in VSCode, with a single shared config for all debuggers that need it.
So there are really two pieces here that require custom IDE-side code right now. One is the custom event handler to create a new session, and the other one is the adapter factory that directs the DAP connection for that to go over a socket. VSCode kinda sorta has the latter in form of "debugServer" already, but since there's no way to specify the hostname, it's only useful for local debugging, not remote. I forgot about that second part, because we use this approach for all "attach" sessions, not just secondary ones, so we implemented it outside of the multi-session scenario.
And yes, you're right - to standardize this in DAP would require it to be aware of the transport layer, since the client can't just spawn adapter to service the reverse request to start a new debug session. Which means that "debugServer" would have to be added to the spec for "launch" and "attach" requests (and extended to accommodate hostname in addition to port), and clients supporting it would be required to implement DAP-over-TCP specifically.
So I guess it depends on how the DAP team views the protocol - is it meant to be entirely transport-agnostic in all respects, or is it accidental and can change if requirements demand it? I think there would be some value in having something as common as TCP be explicitly standardized as a transport. Especially since it'd be opt-in for the clients anyway - it's more about having them interpret the connection information in the same manner if they choose to support it.
The users would likely also appreciate it, since right now adapters that support remote "attach" over TCP don't really have a single consistent format for debug configuration for that, and each has to document it separately. If it's standardized, it could all go straight into the VSCode generic debugging documentation, where users generally land first when they start looking. And eventually, this would allow for SSH tunneling to be transparently added on top.
mickaelistria commentedon Oct 29, 2021
As far as I can see, the multiple session approach is documented in the overview and implemented (with custom messages) in vscode-js-debug.
However, the specification doesn't specify how multiple sessions are supposed to work: how client can start a session, pass a port to use for the child session, which operations (initialize? launch?) need to be sent to the child session...?
As the behavior in vscode-js-debug seems satisfactory, can the related operations and flows be specified?
gfszr commentedon Jul 8, 2022
Any news regarding this issue? Adding support for multi-session debugging for DAP is rather important, and each debug adapter implemeting its own vision of multi-session support creates a lot of fragmentation.
weinand commentedon Jul 9, 2022
Today multi-session debugging happens outside of DAP because that use case must be supported anyway: if I want to debug an application that has processes implemented in C++ and Python, then two debug adapter types are needed and the client (e.g. VS Code) establishes independent sessions with these adapters via debug adapter factories that exist per debug type ("cpp" and "python").
The same approach is used when debugging multiple processes implemented in the same language (where only a single debug adapter type is needed): the client uses a single debug adapter factory to create sessions and it is an implementation detail of the factory (and the debug adapter) whether a single server process is used to implement multiple sessions via multiple communication channels or whether an independent debug adapter process is launched for each session.
With this approach the client (e.g. VS Code) does not have to deal with debug adapters that are able to handle multiple sessions. It just creates a DAP session via a factory and does not have to care about multiplexing DAP sessions over a single comm channel.
gfszr commentedon Jul 9, 2022
Andre Weinand (@weinand) Thank you for the quick reply! 🙂
Considering DAP as the "generalization" of a debugger API, various debuggers (
pydevdandgdb, for instance) allow the concept of "debugging child processes of the same language". However, neither pydevd nor gdb implement a feature of debugging child processes written in arbitrary languages (Well, maybe gdb does :P). If DAP's purpose is to generalize debuggers through a single protocol and session, then one of such features should be supporting debugging same-language child proccesses.Multi-language debugging might sound similar to multi-session debugging, but the latter is a basic feature of a debugger (Thus, should be represented in DAP in some way), while the other is a fancier feature that is out of scope to a debugger (For which, VSCode can solve by using multiple DAP sessions).
Such approach has disadvantages, which I believe DAP was designed to solve: for each debug adapter, the client would need to implement its notion of "multi-session", if one is multiplexing on the same connection or creating new connections. Such example is shown in both
debugpyandvscode-js-debug, which both implement a different approach which is not only non-DAP, but also requires changes in the UI (VScode, or any other editor using DAP for that matter).I'm a huge fan of DAP, but lacking a feature a lot of debuggers implement (multi-processing), defeats the purpose of not reinventing the wheel and write a compatability layer between a DAP server and its UI.
53 remaining items
Proposal: Allow multiple debug sessions through a single adapter (#326)
zobo commentedon Sep 9, 2022
A bit late to the game, but here are my thoughts.
Looking from my own perspective (php-debug) the most important thing is how the DA is spawned from the client (VSCode). Since in my case the first DA instance will listen for incoming TCP connections from PHP/Xdebug and then create a new DA instance for each of them. This means either all of the DAs need to run in the same process (be it VSCode itself or one DA host process) or do something very creative with fork() or some TCP proxying - feels too complex.
Also, I feel that for my use case the user experience is better if the "child" debug sessions are actually NOT hierarchically under the primary "listener" debug session. This way the user can "stop listening" but continue to debug the open sessions.
I understand that this proposal tries to unify how different clients offer creating a new session from a DA instance, but with all the "implementation detail" out of scope we'll see how it will work in VSCode and then across other clients.
Great first steps, happy this is moving along.
connor4312 commentedon Sep 9, 2022
I'm not totally clear on what would change for you in this mode. Looking at php-debug, you use the
programattribute in the package.json, so VS Code will run that Node program for each new client session. If you use the newstartDebuggingrequest, that will continue to happen. Am I misunderstanding your scenario?connor4312 commentedon Sep 9, 2022
Also, I opened the proposal for multiplexing here: #329
zobo commentedon Sep 9, 2022
I should have been more concrete. What I am talking about is not in the main branch, but a separate feature branch, not yet merged.
Here I use the InlineDebugAdapterFactory so that all DA instances run in the same process and can share data in static properties...
What I'm trying to say is that
startDebuggingwill not be enough for some extensions without being able to control how DAs are spawned. I'm looking at your other proposal and this could probably be a good start.Maybe a flag in
package.jsoncould be used to set a spawning mode like:Or a filed in the
startDebuggingreverse request...Keeping this consistent across different clients could be a problem...
jonahgraham commentedon Sep 29, 2022
Can #333 be looked at before 1.59 is finalized?
roblourens commentedon Oct 4, 2022
Due to #333, I'll revert this change in main, and we will finalize it in October. I'd like to implement it in vscode during October.
Add startDebugging request
Add startDebugging request (#344)