You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Socket error: Bad state: Cannot stop a plugin that is not running #4740
Describe the bug
When I open the project in a multi-root worksapce, the dart extension will keep reporting Socket error: Bad state: Cannot stop a plugin that is not running every few seconds. Is there a way to know which plugin this error refers to?
1695276601813:PluginNoti: {
"event": "plugin.error",
"params": {
"isFatal": false,
"message": "Could not start the VM service: localhost:2935 is already in use.\n",
"stackTrace": ""
}
}:file: ///C:/Users/admin/AppData/Local/.dartServer/.plugin_manager/d1d84a9cf669a1228971692ca9be2a97/analyzer_plugin/bin/plugin.dart:
I think this might be happening in the custom_lint plugin. I see a change that may have been intended to fix this at https://github.com/invertase/dart_custom_lint/pull/109/files so I wonder if the issue is there (it's not clear to me if the fix worked or not, as the changelog mentions being reverted). @rrousselGit is this an issue you've had before - do you think this is a custom_lint issue or something the server is doing?
@ViShadowFiend out of interest, are you on the latest version of custom_lint? Could you check which version of custom_lint is recorded in your .dart_tool/package_config.json file (or by running flutter pub deps -s compact | select-string "- custom_lint" from a PowerShell terminal in the project folder)?
The topic is this issue is something I've seen happen quite a few times, but I have no clue on where to even get started with regards to what that could be.
My guess is that it's a VScode thing though. Unless I missed something, I doubt analyzer_plugin enables plugins to show custom snackbars. So it's likely code on your side triggering it.
My assumption is that it may happen when the analyzer restart, which can happen when editing pubspec.yamls for instance.
I'm not sure I understand why they can't be related - the log file shows that a plugin raises the error "Could not start the VM service:: localhost::3094 is already in use." and then there's a fatal error "Failed to start plugins":
1695276620444:PluginNoti:{"event"::"plugin.error","params"::{"isFatal"::false,"message"::"Could not start the VM service:: localhost::3094 is already in use.\n","stackTrace"::""}}:file::///C::/Users/admin/AppData/Local/.dartServer/.plugin_manager/d1d84a9cf669a1228971692ca9be2a97/analyzer_plugin/bin/plugin.dart::
1695276620444:PluginNoti:{"event"::"plugin.error","params"::{"isFatal"::true,"message"::"Failed to start plugins","stackTrace"::""}}:file::///C::/Users/admin/AppData/Local/.dartServer/.plugin_manager/d1d84a9cf669a1228971692ca9be2a97/analyzer_plugin/bin/plugin.dart::
1695276620445:Res:{"jsonrpc"::"2.0","method"::"window/showMessage","params"::{"message"::"Socket error:: Bad state:: Cannot stop a plugin that is not running.","type"::1}}
The server tries to stop the plugin, however that fails - and that error message is surfaced to the user.
There's likely a bug that the server is either stopping the plugin twice, or it is trying to stop a plugin that didn't start - but presumably this chain of events results from the plugin failing to start?
(there's also another error in the log about the dart fix check failing, but that happened after all of this)
My assumption is that it may happen when the analyzer restart, which can happen when editing pubspec.yamls for instance.
The server doesn't restart, it only rebuilds the analysis contexts. There's nothing in the log (which should include watch events) to suggest this was happening at the time though.
@rrousselGit I'm trying to see if I can reproduce this by using the example project from your repo, but I'm struggling to get it to load:
The package "custom_lint_example_lint" has incompatible version constraints in the project:
- "./example_lint"
from "custom_lint_example_app" at ".\..\..\..\..\..\..\Dev\Test Projects\custom_lint_testing\dart_custom_lint\packages\custom_lint\example\pubspec.yaml".
The constraints were Dart 2-3, so I changed them to 3-4, but I still see this error. Pubspec looks like this:
I have tried to remove custom_lint from pubspec and analysis_options, and now the analyzer is running normally
But the strange thing is that during this period, vscode could not analyze/run normally due to this problem. I switched to android studio, and android studio analyzed/run normally.
Looks like your custom_lint is out of date (the current version is 0.5.3). Can you try running flutter pub upgrade to see whether it updates to the latest, and see whether that solves the issue?
But the strange thing is that during this period, vscode could not analyze/run normally due to this problem. I switched to android studio, and android studio analyzed/run normally.
I'm not sure why the difference - if I'm able to reproduce this I can try to do some debugging, but so far I wasn't able to get the custom_lint example project to work (#4740 (comment)).
@DanTup I have no problem running custom_lint's examples.
Chances are you shouldn't modify the SDK constraints. If you forgot one place, custom_lint could complain that some projects in your workspace have incompatible SDK constraints.
It currently doesn't treat Dart <3.0.0 as <4.0.0. So if one project has >2<3 and another has >3<4 then you'll get an error :)
Anyway I've updated all constraints to <4 just in case. Maybe try again
@rrousselGit thanks - it was not being able to use v3 that caused my to change them. Pulling your latest changes seems to work though.
@ViShadowFiend unfortunately I've been unable to reproduce this with a clean latest clone of the custom_lint repo, opening two copies of the example folder (I copied it to example2 and updated the project names).
Are you able to share a small (but complete) sample project/folder that is triggering this? Ideally, with all the .dart_tool folders in-tact, and a saved .code-workspace folder that opens things exactly as reproduces the issue for you.
// Unable to edit document because the file was not previously opened: D:\coding\socketerror\lib_ble\example\lib\main.dart
// Socket error: Concurrent modification during iteration: _Map len:2.
// An unrecoverable error occurred and the server cannot process messages
Maybe it's because I used another computer or new Dart extension this time
Operating System and version: win10 10.0.19045.3208
Thanks for the updated log. I can see a few weird things in there... one is that analysis is failing for a file (this is usually a bug in parsing or something):
*** 1701018908962:Res:{"id"::36,"jsonrpc"::"2.0","error"::{"code"::-32013,"data"::"D::\coding\dartcode_error_f\lib_ble\example\lib\app.dart","message"::"Analysis failed for file"}}
And next I see a file closed and re-opened before we send this error:
@ViShadowFiend I'm not having much look reproducing these issues. Can you confirm exactly what code you started with (including any changes made from what's in the repository)? I noticed that custom_lint is commented out in the repo but your log shows that the plugin is enabled.
From the logs, it looks like the first error (analysis failed) when you typed a T in the file example\lib\app.dart here:
I tried:
switching to Flutter 3.16.0
clean clone of the project
Allow VS Code to run pub get in all projects
I noticed it got newer versions of some packages, so I hard-coded all the versions to match what was in your package_config.json and ran it again until all versions in package_config matched the versions you had committed
enabled the custom_lint plugin in example/analysis_options.yaml
opened your .code-workspace file and made the change above in example/lib/app.dart (and tried saving)
Unfortunately none of the issues in your log occurred in mine. If you can provide precise steps from cloning the repository (including any modifications to any files) that might help me reproduce. Thanks!
I just added two new commits, please check them again.
Let me describe the reproduction process
Clean clone from the second to last commit
Open vLibs.code-workspace
A message pops up in the lower right corner of vscode, confirm to run flutter pub get
Enable plugins - custom_lint, save analysis_options.yaml file
Open lib_ble/example/lib/app.dart and edit, error occurs
The last commit has custom_lint enabled, and the dartcode_error/lib_ble/logs
/analyzer.txt.1 file contains the Unable to edit document because the file was not previously opened error message
Unfortunately it still doesn't reproduce. Is it possible you could send me a copy of the following files?
From C:\Users\Vi\AppData\Local\.dartServer\.plugin_manager\ada8d5e66ea6c5757d0231fed119e521:
analyzer_plugin\.dart_tool\package_config.json
analyzer_plugin\pubspec.lock
analyzer_plugin\pubspec.yaml
While I'm on the same versions of all packages in your project, perhaps there's a difference in the versions that were resolved for the plugins. I'm not sure why that would cause this kind of failure, but perhaps by eliminating more variables I'll be able to trigger it here.
name: custom_lint_analyzer_plugin_loader
description: This pubspec determines the version of the analyzer plugin to load.
version: 0.5.7
publish_to: none
environment:
sdk: ">=2.14.0 <4.0.0"
dependencies:
custom_lint: 0.5.7
# TODO: If you want to contribute to custom_lint, add a pubspec_overrides.yaml file
# in this folder, containing the following:
# dependency_overrides:
# custom_lint:
# path: /absolute/path/to/custom_lint/folder
@ViShadowFiend thanks! Turns out those versions all matched mine so it wasn't that. I've still not been able to repro (I even tried replaying your log against a local server), however when reading through the logs I noticed that the plugin was being started/shutdown when I wouldn't except (when you closed a file).
I noticed you have the "dart.onlyAnalyzeProjectsWithOpenFiles" setting set to true in your User Settings. Can you try disabling that and reloading VS Code and see whether you can still reproduce without that setting? I can't repro even with that set, but it'd be interesting to know whether it's contributing to the issue.
(I'm also curious what led you to enable that setting - it was mostly there for non-VS Code LSP clients that don't have a good concept of an open workspace).
Actually, I was able to reproduce the issue with that setting enabled manually (probably the timing when I replayed the log was not the same). I should be able to track it down from here - thank you for the help with the logs!
I raised #4862 for the "Unable to edit document because the file was not previously opened" issue specifically (because I'm unsure if there are still other errors here), but here's a summary:
The issue is a bug in handling of closed files. There is an await that meant if a client closed and re-opened a file very close together, the close could be processed after the open, leaving the file in a "closed" state (which produces this error on the next change).
It isn't directly related to plugins or onlyAnalyzeProjectsWithOpenFiles, however using plugins can result in us re-registering capabilities with the client when analysis roots change (which results in VS Code's LSP client closing and re-opening all files since those events are tied to the capabilities) and using onlyAnalyzeProjectsWithOpenFiles causes additional work when files are opening/closing which makes it more likely for the bad await to cause the work to be re-ordered.
I have a fix open at https://dart-review.googlesource.com/c/sdk/+/338660 but since it'll only ship in a future SDK release my recommendation is to disable onlyAnalyzeProjectsWithOpenFiles as a workaround (I'd generally recommend not using that at all, but if you do feel you need it for some projects, set it only on those projects rather than in your global user settings).
On the topic of errors, should VScode show errors from analyzer_plugins in a snackbar?
I'm not sure, though I suspect the answer might depend on the kind/frequency of errors. If a plugin is completely broken (throwing exceptions) I think there should be a way for a user to know this, but we never want to be in the situation where a plugin is throwing errors on every keystroke and we're spewing out notifications (something I've seen in Dart-Code in the past without plugins and it looks terrible).
Something we don't use yet but might be an idea place for errors like that (and other non-critical but severe errors from the server) in VS Code is the language status area. Setting an error/warning status would show an indicator on the "{} Dart" area of the status bar, and opening the flyout would show a little more info (with a link to the analyzer diagnostics page with more details).
I probably don't have a good handle on the frequency (or severity) of the errors plugins generate, but if the current behaviour doesn't seem ideal, perhaps it's worth opening an issue in the SDK for discussion (it's probably not worth making any changes prior to the work @srawlins is doing though).
I'm glad you finally reproduced the error! Thank you very much for your patient replies!
I just disabled the dart.onlyAnalyzeProjectsWithOpenFiles option, and indeed there are no more errors, everything is fine!
As for why I enabled this option, I remember it was because the dart analyzer analyzes the dart files of the entire project, which is so slow that I have to wait for a long time every time I open the project.
As for why I enabled this option, I remember it was because the dart analyzer analyzes the dart files of the entire project, which is so slow that I have to wait for a long time every time I open the project.
With this setting, the analyzer will still analyze whole projects - but only those projects that have open files. If you're using something like a command line editor which might set a very large workspace folder, this can reduce the analysis done. For something like VS Code, you'll incur the cost of destroying and re-creating analysis contexts as you close/open files though, so you might find that analysis is less responsive immediately after opening the "first" file in a project each time.
If you do feel you need to use it for some projects (though I'd still generally advise against it - and file issues if you feel the performance is bad with some details about the size of your workspace), I would suggesting setting it only in their workspaces (and not your user settings).
Activity
DanTup commentedon Sep 13, 2023
There's not enough information here to tell which plugin is causing the issue. The full stack trace is:
And this seems to occur when an error occurs in a plugin.
Could you try enabling the analysis server instrumentation log (which might record the plugin error):
F1to open the command palette"dart.analyzerInstrumentationLogFile": "logs/analyzer.txt"pubspec.yaml/analysis_options.yamlfilesPlease be sure to remote anything sensitive from the log/files or reproduce the issue on a non-sensitive project.
ViShadowFiend commentedon Sep 21, 2023
@DanTup
hello! I'm sorry I just saw the reply.
this is log
logs/analyzer.txt
this is pubspec.yaml
pubspec
this is analysis_options
analysis_options
DanTup commentedon Sep 21, 2023
In the log I see this error being generated:
I think this might be happening in the custom_lint plugin. I see a change that may have been intended to fix this at https://github.com/invertase/dart_custom_lint/pull/109/files so I wonder if the issue is there (it's not clear to me if the fix worked or not, as the changelog mentions being reverted). @rrousselGit is this an issue you've had before - do you think this is a
custom_lintissue or something the server is doing?DanTup commentedon Sep 21, 2023
I found mention of this same error at invertase/dart_custom_lint#115 (comment) and Remi responded that it was fixed.
@ViShadowFiend out of interest, are you on the latest version of custom_lint? Could you check which version of
custom_lintis recorded in your.dart_tool/package_config.jsonfile (or by runningflutter pub deps -s compact | select-string "- custom_lint"from a PowerShell terminal in the project folder)?rrousselGit commentedon Sep 21, 2023
@DanTup The linked issue is unrelated.
The topic is this issue is something I've seen happen quite a few times, but I have no clue on where to even get started with regards to what that could be.
My guess is that it's a VScode thing though. Unless I missed something, I doubt analyzer_plugin enables plugins to show custom snackbars. So it's likely code on your side triggering it.
My assumption is that it may happen when the analyzer restart, which can happen when editing pubspec.yamls for instance.
DanTup commentedon Sep 21, 2023
I'm not sure I understand why they can't be related - the log file shows that a plugin raises the error "Could not start the VM service:: localhost::3094 is already in use." and then there's a fatal error "Failed to start plugins":
The server tries to stop the plugin, however that fails - and that error message is surfaced to the user.
There's likely a bug that the server is either stopping the plugin twice, or it is trying to stop a plugin that didn't start - but presumably this chain of events results from the plugin failing to start?
(there's also another error in the log about the
dart fixcheck failing, but that happened after all of this)The server doesn't restart, it only rebuilds the analysis contexts. There's nothing in the log (which should include
watchevents) to suggest this was happening at the time though.DanTup commentedon Sep 21, 2023
@rrousselGit I'm trying to see if I can reproduce this by using the example project from your repo, but I'm struggling to get it to load:
The constraints were Dart 2-3, so I changed them to 3-4, but I still see this error. Pubspec looks like this:
I don't know if you have access to the specific versions that are wrong, but perhaps this error could include them to make it easier to track down?
Edit: Nevermind, deleting the plugin-manager folder from .dartServer seems to have fixed it.. I guess something was stale in there.Edit2: Spoke too soon, it's still there.
ViShadowFiend commentedon Sep 22, 2023
Sorry, what I provided before was
analysis_optionsof libthis is
analysis_optionsof example holdermy current custom_lint version is 0.4.0 from
.dart_tool/package_config.jsonI have tried to remove
custom_lintfrom pubspec andanalysis_options, and now the analyzer is running normallyBut the strange thing is that during this period, vscode could not analyze/run normally due to this problem. I switched to android studio, and android studio analyzed/run normally.
DanTup commentedon Sep 22, 2023
Looks like your
custom_lintis out of date (the current version is 0.5.3). Can you try runningflutter pub upgradeto see whether it updates to the latest, and see whether that solves the issue?I'm not sure why the difference - if I'm able to reproduce this I can try to do some debugging, but so far I wasn't able to get the
custom_lintexample project to work (#4740 (comment)).ViShadowFiend commentedon Sep 22, 2023
@DanTup
I tried to change
to
Will still report the
Socket error: Bad state: Cannot stop a plugin that is not runningerrorrrousselGit commentedon Sep 24, 2023
@DanTup I have no problem running custom_lint's examples.
Chances are you shouldn't modify the SDK constraints. If you forgot one place, custom_lint could complain that some projects in your workspace have incompatible SDK constraints.
It currently doesn't treat Dart <3.0.0 as <4.0.0. So if one project has >2<3 and another has >3<4 then you'll get an error :)
Anyway I've updated all constraints to <4 just in case. Maybe try again
DanTup commentedon Oct 4, 2023
@rrousselGit thanks - it was not being able to use v3 that caused my to change them. Pulling your latest changes seems to work though.
@ViShadowFiend unfortunately I've been unable to reproduce this with a clean latest clone of the custom_lint repo, opening two copies of the
examplefolder (I copied it toexample2and updated the project names).Are you able to share a small (but complete) sample project/folder that is triggering this? Ideally, with all the
.dart_toolfolders in-tact, and a saved.code-workspacefolder that opens things exactly as reproduces the issue for you.Thanks!
ViShadowFiend commentedon Oct 4, 2023
@DanTup
I create a repo here: https://github.com/ViShadowFiend/dartcode_error
But now, I can't reproduce the
Socket error: Bad state: Cannot stop a plugin that is not runningerror, Now there are 3 more new errors.Maybe it's because I used another computer or new Dart extension this time
40 remaining items
DanTup commentedon Nov 27, 2023
Thanks for the updated log. I can see a few weird things in there... one is that analysis is failing for a file (this is usually a bug in parsing or something):
And next I see a file closed and re-opened before we send this error:
This also seems incorrect, because our error message says the file is not open, but according to the log it clearly is.
I'll see what I can find, thanks!
DanTup commentedon Nov 28, 2023
@ViShadowFiend I'm not having much look reproducing these issues. Can you confirm exactly what code you started with (including any changes made from what's in the repository)? I noticed that custom_lint is commented out in the repo but your log shows that the plugin is enabled.
From the logs, it looks like the first error (analysis failed) when you typed a
Tin the fileexample\lib\app.darthere:I tried:
pub getin all projectspackage_config.jsonand ran it again until all versions in package_config matched the versions you had committedexample/analysis_options.yaml.code-workspacefile and made the change above inexample/lib/app.dart(and tried saving)Unfortunately none of the issues in your log occurred in mine. If you can provide precise steps from cloning the repository (including any modifications to any files) that might help me reproduce. Thanks!
ViShadowFiend commentedon Nov 28, 2023
@DanTup
I just added two new commits, please check them again.
Let me describe the reproduction process
The last commit has custom_lint enabled, and the dartcode_error/lib_ble/logs
/analyzer.txt.1 file contains the
Unable to edit document because the file was not previously openederror messageDanTup commentedon Nov 28, 2023
Unfortunately it still doesn't reproduce. Is it possible you could send me a copy of the following files?
From
C:\Users\Vi\AppData\Local\.dartServer\.plugin_manager\ada8d5e66ea6c5757d0231fed119e521:analyzer_plugin\.dart_tool\package_config.jsonanalyzer_plugin\pubspec.lockanalyzer_plugin\pubspec.yamlWhile I'm on the same versions of all packages in your project, perhaps there's a difference in the versions that were resolved for the plugins. I'm not sure why that would cause this kind of failure, but perhaps by eliminating more variables I'll be able to trigger it here.
Thanks!
ViShadowFiend commentedon Nov 28, 2023
@DanTup
analyzer_plugin\.dart_tool\package_config.json
analyzer_plugin\pubspec.lock
analyzer_plugin\pubspec.yaml
DanTup commentedon Nov 28, 2023
@ViShadowFiend thanks! Turns out those versions all matched mine so it wasn't that. I've still not been able to repro (I even tried replaying your log against a local server), however when reading through the logs I noticed that the plugin was being started/shutdown when I wouldn't except (when you closed a file).
I noticed you have the
"dart.onlyAnalyzeProjectsWithOpenFiles"setting set totruein your User Settings. Can you try disabling that and reloading VS Code and see whether you can still reproduce without that setting? I can't repro even with that set, but it'd be interesting to know whether it's contributing to the issue.(I'm also curious what led you to enable that setting - it was mostly there for non-VS Code LSP clients that don't have a good concept of an open workspace).
Thanks!
DanTup commentedon Nov 28, 2023
Actually, I was able to reproduce the issue with that setting enabled manually (probably the timing when I replayed the log was not the same). I should be able to track it down from here - thank you for the help with the logs!
DanTup commentedon Nov 28, 2023
I raised #4862 for the "Unable to edit document because the file was not previously opened" issue specifically (because I'm unsure if there are still other errors here), but here's a summary:
The issue is a bug in handling of closed files. There is an
awaitthat meant if a client closed and re-opened a file very close together, the close could be processed after the open, leaving the file in a "closed" state (which produces this error on the next change).It isn't directly related to plugins or
onlyAnalyzeProjectsWithOpenFiles, however using plugins can result in us re-registering capabilities with the client when analysis roots change (which results in VS Code's LSP client closing and re-opening all files since those events are tied to the capabilities) and usingonlyAnalyzeProjectsWithOpenFilescauses additional work when files are opening/closing which makes it more likely for the badawaitto cause the work to be re-ordered.I have a fix open at https://dart-review.googlesource.com/c/sdk/+/338660 but since it'll only ship in a future SDK release my recommendation is to disable
onlyAnalyzeProjectsWithOpenFilesas a workaround (I'd generally recommend not using that at all, but if you do feel you need it for some projects, set it only on those projects rather than in your global user settings).rrousselGit commentedon Nov 28, 2023
On the topic of errors, should VScode show errors from analyzer_plugins in a snackbar?
DanTup commentedon Nov 28, 2023
I'm not sure, though I suspect the answer might depend on the kind/frequency of errors. If a plugin is completely broken (throwing exceptions) I think there should be a way for a user to know this, but we never want to be in the situation where a plugin is throwing errors on every keystroke and we're spewing out notifications (something I've seen in Dart-Code in the past without plugins and it looks terrible).
Something we don't use yet but might be an idea place for errors like that (and other non-critical but severe errors from the server) in VS Code is the language status area. Setting an error/warning status would show an indicator on the "{} Dart" area of the status bar, and opening the flyout would show a little more info (with a link to the analyzer diagnostics page with more details).
I probably don't have a good handle on the frequency (or severity) of the errors plugins generate, but if the current behaviour doesn't seem ideal, perhaps it's worth opening an issue in the SDK for discussion (it's probably not worth making any changes prior to the work @srawlins is doing though).
[analysis_server] Ensure textDocument/didClose removes overlays synch…
ViShadowFiend commentedon Nov 29, 2023
I'm glad you finally reproduced the error! Thank you very much for your patient replies!
I just disabled the
dart.onlyAnalyzeProjectsWithOpenFilesoption, and indeed there are no more errors, everything is fine!As for why I enabled this option, I remember it was because the dart analyzer analyzes the dart files of the entire project, which is so slow that I have to wait for a long time every time I open the project.
Thanks again!
DanTup commentedon Nov 29, 2023
With this setting, the analyzer will still analyze whole projects - but only those projects that have open files. If you're using something like a command line editor which might set a very large workspace folder, this can reduce the analysis done. For something like VS Code, you'll incur the cost of destroying and re-creating analysis contexts as you close/open files though, so you might find that analysis is less responsive immediately after opening the "first" file in a project each time.
If you do feel you need to use it for some projects (though I'd still generally advise against it - and file issues if you feel the performance is bad with some details about the size of your workspace), I would suggesting setting it only in their workspaces (and not your user settings).