Support restart
in terminated event in debug adapter
#1218
Replies: 27 comments
-
Thank you for the suggestion! We have marked this issue as "needs decision" to make sure we have a conversation about your idea. We plan to leave this feature request open for at least a month to see how many 👍 votes the opening comment gets to help us make our decision. |
Beta Was this translation helpful? Give feedback.
-
Hello, just to give a few data points/verbatim from other github threads(microsoft/vscode-azurefunctions#781 & Azure/azure-functions-host#3543) regarding customer pain points that this change might alleviate Thanks! |
Beta Was this translation helpful? Give feedback.
-
Adding +1 on this - we have many customers in Azure Functions Python VSCode experience |
Beta Was this translation helpful? Give feedback.
-
Hello @karthiknadig, do you think there is already enough data points for this work item? Thanks! |
Beta Was this translation helpful? Give feedback.
-
@stefanushinardi Thanks for the data points. When python files are edited, which process is doing the reload? is there a managing process that handles terminating and reloading the project. If yes, then is that process running on python? |
Beta Was this translation helpful? Give feedback.
-
Pinging @nturinski and @Hazhzeng who are the experts on this |
Beta Was this translation helpful? Give feedback.
-
I honestly rarely work with Python so I really don't know for sure, but I think VS Code is using this repo to do its debugging: https://github.com/microsoft/debugpy. So I imagine that there should be a way to watch the files via the debugpy process while its debugging and then have the Python extension reattach after a change. |
Beta Was this translation helpful? Give feedback.
-
@nturinski The issue is we don't know which process it is if the restarted python process was not started by a parent that was already running under the debugger. I am one of the authors of If there is no main process that serves as a root (with debugger running in it), then it becomes difficult to know when the new processes gets spawned. There is no way from the debugger to tell this from outside of the newly spawned process. It could be a unrelated python process. Another issue is, how do we know that a debugging session is over? how long does it take from process terminating to a new process start? The only direction that could potentially work in this case is the reverse direction, that is the launched process some how starts debug session (this is not supported and not a small work item). I am not sure how that scenario will work with vscode server. |
Beta Was this translation helpful? Give feedback.
-
My original question is still un-answered. Is there a process that you have that restarts the server? if yes then is that process a python process? if yes then can that process itself be run under the debugger? |
Beta Was this translation helpful? Give feedback.
-
Note that the original Node.js scenario that is referenced is an attach scenario - the user explicitly runs some code under So if I'm understanding the feature request correctly, it's a debug configuration property that would cause the debugger to immediately try to re-attach if the debug session ends, using the same exact debug config. Note however that this is not idiomatic in the Python ecosystem. That is, there's no generic |
Beta Was this translation helpful? Give feedback.
-
Our scenario is for debugging Azure Functions, which supports several languages (Python, C#, JavaScript, etc.). The core runtime is built in .NET Core and that is what handles managing and/or restarting the various language-specific processes (what they call "workers"). So no, I don't think we can just attach to a "root" process because it is not implemented in Python.
Yes, but when you say "if the debug session ends" the Node.js debugger seems to know the difference between "the debug port unexpectedly closed" vs "the user manually detached" and will not try to reattach in the latter scenario.
As @int19h mentioned, we're focused specifically on the attach scenario. I expect the same timeout/waiting logic would apply for the first attach vs. the second attach. |
Beta Was this translation helpful? Give feedback.
-
I had a look at how the Node debug adapter does this. It seems to boil down to using "restart" in the "terminated" event, the presence of which causes VSCode to restart the session and pass the value of "restart" in the new debug config as "__restart". And the adapter only does it if the session ends by itself, and not if it receives a "terminate" request. It sounds like something we could implement fairly easily in debugpy following the same pattern. |
Beta Was this translation helpful? Give feedback.
-
Moving this to |
Beta Was this translation helpful? Give feedback.
-
@ejizba @nturinski thanks for the details |
Beta Was this translation helpful? Give feedback.
-
I'll start to work on this issue. I'll rephrase what I understood (so that someone can correct me if I got the wrong idea). The idea is that given an attach launch configuration that has |
Beta Was this translation helpful? Give feedback.
-
@fabioz That is correct. |
Beta Was this translation helpful? Give feedback.
-
To give some feedback, I tried just passing the The debug adapter in this case is started by the server (python process) as a separate process and starts communicating with VSCode at the specified port (say When it sends the This means that the new connection made fails when trying to start a new adapter to listen in the specified port ( I tried sending the Also, I can see that if the adapter process is not killed, VSCode itself keeps the connection open and issues a new This doesn't work due to 2 things:
So, I need to investigate a bit more on why VSCode is trying to reuse that connection (I thought that VSCode would just disconnect from |
Beta Was this translation helpful? Give feedback.
-
I finished my investigation on this... Fixing this will need changes on both What was happening is that The problem is that later on, if the connection isn't readily available (which happens because the process that'll listen again takes a bit of time to start-up and get to i.e.: in: https://github.com/microsoft/vscode-python/blob/2021.6.944021595/src/client/debugger/extension/adapter/factory.ts#L50, it uses a So, given that I'll proceed to do the changes on |
Beta Was this translation helpful? Give feedback.
-
Note: I'll have leave this on hold for a bit... Some mental notes for when I (or someone else) checks it:
|
Beta Was this translation helpful? Give feedback.
-
I actually wonder if we can make this work without respawning the adapter, rather than trying to hack around the inherent problems with asynchronously connecting to it. So far as I can tell, there are two problems with this in the existing implementation:
The first problem should be straightforward to handle by resetting all the affected state. The second one is trickier, but I think it's possible for the new server and the original adapter to coordinate. While the new server doesn't know the port on which it should connect to the adapter as a server, it does know the port on which the adapter will be listening for clients, since that's passed to For that matter, separate ports might not even be necessary. We currently use them to distinguish incoming client and server connections, but this can also be done based on the first message received on it - the server will simply send a custom one identifying it as such, and the adapter listener will then create an instance of |
Beta Was this translation helpful? Give feedback.
-
We also need to consider the scenario where there are two independent but overlapping debug sessions, each with its own adapter. If the user forgets to specify different port numbers, currently, this will fail and report an error when they try to start the second server. But if we do the above, it will end up quietly attaching to the adapter from the first session, and the whole thing will be treated as one large multiproc session, which would be rather surprising. The problem is that the adapter is unable to distinguish between the servers to figure out if the server connecting to it is a new instance that logically corresponds to another session that ended earlier, or it's a completely new one. If the actual restart process provided some facility to pass information from the old server to the new one (like Or perhaps this is best handled by using |
Beta Was this translation helpful? Give feedback.
-
So as someone who just came here - I see you guys are all the way in a discussion about how it could be implemented and seeing the last update is a year ago - I thought I check where the feature request is at. I am running Django inside docker and it is an attached process. As an outsider, I do not see why this causes the debuggy process to close. Or should this already work and my configs are just wrong? |
Beta Was this translation helpful? Give feedback.
-
We still don't support "restart", so if your scenario relies on that, it would be expected to not work. |
Beta Was this translation helpful? Give feedback.
-
@int19h I was thinking about taking a look into the approach of doing the restart while keeping the debug adapter process live. Is this Ok or do you have plans to work on that already? |
Beta Was this translation helpful? Give feedback.
-
@fabioz I have some WIP code for this already. |
Beta Was this translation helpful? Give feedback.
-
Based on the discussion above and in #1212, I'm going to handle this piecemeal. Specifically, the "attach"{"listen"} scenario (i.e. where the debuggee connects to the client) will be first - it's much simpler and with fewer moving bits to synchronize because VSCode is responsible for adapter lifetime in that case; yet it also seemingly covers all the practical scenarios. The complexity induced by the requirements of doing this for "attach"{"connect"} (i.e. when the debuggee is listening) does not feel like it's worth it, and it's not clear wrt "launch", either. |
Beta Was this translation helpful? Give feedback.
-
Hello from 2024! Seeing as how #577 has been closed one might wonder if the changes mentioned by @int19h were ever implemented and/or released. I assume not because there's no update here, but it would be nice if I were wrong. Anything new on this front? |
Beta Was this translation helpful? Give feedback.
-
It would be really handy if Python launch configurations had a similar property to Node.js's restart property. For reference: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_restarting-debug-sessions-automatically-when-source-is-edited.
The use case is whenever a python file is edited and saved, it causes the debugger to detach so it'd be nice to have an option to automatically re-attach the debugger in those instances.
Beta Was this translation helpful? Give feedback.
All reactions