Plugin install fails with context canceled because HTTP request context is used in background goroutine #245

Open
opened 2026-02-16 01:15:07 -05:00 by yindo · 1 comment
Owner

Originally created by @IthacaDream on GitHub (Feb 7, 2026).

Self Checks

To make sure we get to you in time, please check the following :)

  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • "Please do not modify this template :) and fill in all the required fields."

Versions

  1. dify-plugin-daemon Version 0.5.3
  2. dify-api Version 1.12.1

Describe the bug
When installing a plugin on local runtime, dependency installation (uv sync) may fail
immediately with:

  • failed to start command: context canceled
  • failed to install dependencies ...
  • failed to init environment

Root Cause
The plugin installation is executed asynchronously (background goroutine) but it reuses c.Request.Context() from the HTTP handler.
Once the handler returns (after responding with a task id), the request context is canceled, which cancels exec.CommandContext(...)
used to run uv sync, causing cmd.Start() to fail with context canceled.

To Reproduce
Steps to reproduce the behavior:

 controller -> service.InstallMultiplePluginsToTenant(c.Request.Context()) -> routine.Submit(...) -> tasks.ProcessInstallJob(ctx) -> manager.Install(ctx) -> control_panel.LaunchLocalPlugin(ctx) -> runtime.InitEnvironment(...) -> setup_python_environment.go:exec.CommandContext(ctx, uvPath, ...)

Expected behavior
Detach from the request cancellation when starting background install/upgrade tasks:

Screenshots

Additional context

2026-02-07T09:33:43.099175Z INFO dify-plugin-daemon setup_python_environment.go:153 installing plugin dependencies plugin=langgenius/azure_openai:0.0.36 method="uv sync" file=pyproject.toml                                                                               
2026-02-07T09:33:43.099235Z ERROR dify-plugin-daemon logger.go:24 local runtime start failed plugin=langgenius/azure_openai:0.0.36@28b
f4e464f55c23fa3ea9a44126a7da6064b131b3e7440fd39a2253bdb4f7879 error="failed to install dependencies: failed to start command: context canceled\nfailed to init environment"                                                                                                 
github.com/langgenius/dify-plugin-daemon/internal/core/control_panel.(*StandardLogger).OnLocalRuntimeStartFailed                      
        /dify-plugin-daemon/internal/core/control_panel/logger.go:24                                                  
github.com/langgenius/dify-plugin-daemon/internal/core/control_panel.(*ControlPanel).LaunchLocalPlugin.func6                          
        /dify-plugin-daemon/internal/core/control_panel/launcher_local.go:101                                         
github.com/langgenius/dify-plugin-daemon/internal/core/control_panel.(*ControlPanel).WalkNotifiers                                    
        /dify-plugin-daemon/internal/core/control_panel/notifier.go:20                                                
github.com/langgenius/dify-plugin-daemon/internal/core/control_panel.(*ControlPanel).LaunchLocalPlugin                                
        /dify-plugin-daemon/internal/core/control_panel/launcher_local.go:100
github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager.(*PluginManager).installLocal.func1
        /dify-plugin-daemon/internal/core/plugin_manager/installer.go:323
github.com/langgenius/dify-plugin-daemon/pkg/utils/routine.Submit.func1.1
        /dify-plugin-daemon/pkg/utils/routine/pool.go:68
github.com/langgenius/dify-plugin-daemon/pkg/utils/routine.Submit.func1
        /dify-plugin-daemon/pkg/utils/routine/pool.go:58
github.com/panjf2000/ants/v2.(*goWorker).run.func1
        /go/pkg/mod/github.com/panjf2000/ants/v2@v2.10.0/worker.go:71```

Originally created by @IthacaDream on GitHub (Feb 7, 2026). **Self Checks** To make sure we get to you in time, please check the following :) - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify-plugin-daemon/issues), including closed ones. - [x] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] "Please do not modify this template :) and fill in all the required fields." **Versions** 1. dify-plugin-daemon Version 0.5.3 2. dify-api Version 1.12.1 **Describe the bug** When installing a plugin on local runtime, dependency installation (uv sync) may fail immediately with: - failed to start command: context canceled - failed to install dependencies ... - failed to init environment Root Cause The plugin installation is executed asynchronously (background goroutine) but it reuses c.Request.Context() from the HTTP handler. Once the handler returns (after responding with a task id), the request context is canceled, which cancels exec.CommandContext(...) used to run uv sync, causing cmd.Start() to fail with context canceled. **To Reproduce** Steps to reproduce the behavior: ```text controller -> service.InstallMultiplePluginsToTenant(c.Request.Context()) -> routine.Submit(...) -> tasks.ProcessInstallJob(ctx) -> manager.Install(ctx) -> control_panel.LaunchLocalPlugin(ctx) -> runtime.InitEnvironment(...) -> setup_python_environment.go:exec.CommandContext(ctx, uvPath, ...) ``` **Expected behavior** Detach from the request cancellation when starting background install/upgrade tasks: **Screenshots** **Additional context** ``` 2026-02-07T09:33:43.099175Z INFO dify-plugin-daemon setup_python_environment.go:153 installing plugin dependencies plugin=langgenius/azure_openai:0.0.36 method="uv sync" file=pyproject.toml 2026-02-07T09:33:43.099235Z ERROR dify-plugin-daemon logger.go:24 local runtime start failed plugin=langgenius/azure_openai:0.0.36@28b f4e464f55c23fa3ea9a44126a7da6064b131b3e7440fd39a2253bdb4f7879 error="failed to install dependencies: failed to start command: context canceled\nfailed to init environment" github.com/langgenius/dify-plugin-daemon/internal/core/control_panel.(*StandardLogger).OnLocalRuntimeStartFailed /dify-plugin-daemon/internal/core/control_panel/logger.go:24 github.com/langgenius/dify-plugin-daemon/internal/core/control_panel.(*ControlPanel).LaunchLocalPlugin.func6 /dify-plugin-daemon/internal/core/control_panel/launcher_local.go:101 github.com/langgenius/dify-plugin-daemon/internal/core/control_panel.(*ControlPanel).WalkNotifiers /dify-plugin-daemon/internal/core/control_panel/notifier.go:20 github.com/langgenius/dify-plugin-daemon/internal/core/control_panel.(*ControlPanel).LaunchLocalPlugin /dify-plugin-daemon/internal/core/control_panel/launcher_local.go:100 github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager.(*PluginManager).installLocal.func1 /dify-plugin-daemon/internal/core/plugin_manager/installer.go:323 github.com/langgenius/dify-plugin-daemon/pkg/utils/routine.Submit.func1.1 /dify-plugin-daemon/pkg/utils/routine/pool.go:68 github.com/langgenius/dify-plugin-daemon/pkg/utils/routine.Submit.func1 /dify-plugin-daemon/pkg/utils/routine/pool.go:58 github.com/panjf2000/ants/v2.(*goWorker).run.func1 /go/pkg/mod/github.com/panjf2000/ants/v2@v2.10.0/worker.go:71``` ```
yindo added the buggo labels 2026-02-16 01:15:07 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Feb 7, 2026):

The failure happens because the plugin installation background goroutine reuses the HTTP request context, which is canceled as soon as the handler responds. This causes any long-running operations (like dependency installation via exec.CommandContext) to be killed prematurely with "context canceled" errors. The code path confirms that c.Request.Context() is passed all the way from the HTTP handler to the background task without detachment, so the background work is tied to the HTTP request lifecycle and gets canceled too soon see code.

The correct fix is to detach the background task from the HTTP request context. This was addressed in PR #596 by using context.WithoutCancel(ctx) (or context.Background()) when starting background work, so the install/upgrade process is not interrupted when the HTTP handler returns. If you are on v0.5.3 or earlier, you should update your code to ensure background tasks do not use the request-scoped context for any work that must outlive the HTTP request.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Feb 7, 2026): <!-- Answer --> The failure happens because the plugin installation background goroutine reuses the HTTP request context, which is canceled as soon as the handler responds. This causes any long-running operations (like dependency installation via exec.CommandContext) to be killed prematurely with "context canceled" errors. The code path confirms that c.Request.Context() is passed all the way from the HTTP handler to the background task without detachment, so the background work is tied to the HTTP request lifecycle and gets canceled too soon [see code](https://github.com/langgenius/dify-plugin-daemon/blob/f272e6c3fcd8a18c6ff571d7fcbfde857f63ef99/internal/service/install_plugin.go#L35-L142). The correct fix is to detach the background task from the HTTP request context. This was addressed in [PR #596](https://github.com/langgenius/dify-plugin-daemon/pull/596) by using context.WithoutCancel(ctx) (or context.Background()) when starting background work, so the install/upgrade process is not interrupted when the HTTP handler returns. If you are on v0.5.3 or earlier, you should update your code to ensure background tasks do not use the request-scoped context for any work that must outlive the HTTP request. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/8cbcc2fd-06d4-4271-9f2c-acd43df039cb?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/8cbcc2fd-06d4-4271-9f2c-acd43df039cb?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/8cbcc2fd-06d4-4271-9f2c-acd43df039cb?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/8cbcc2fd-06d4-4271-9f2c-acd43df039cb?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/8cbcc2fd-06d4-4271-9f2c-acd43df039cb?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/8cbcc2fd-06d4-4271-9f2c-acd43df039cb?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/8cbcc2fd-06d4-4271-9f2c-acd43df039cb?feedback_type=other)</sup> [![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=The%20failure%20happens%20because%20the%20plugin%20installation%20background%20goroutine%20reuses%20the%20HTTP%20request%20context%2C%20which%20is%20canceled%20as%20soon%20as%20the%20handler%20responds.%20This%20causes%20any%20long-running%20operations%20%28like%20dependency%20installation%20via%20exec.CommandContext%29%20to%20be%20killed%20prematurely%20with%20%22context%20canceled%22%20errors.%20The%20code%20path%20confirms%20that%20c.Request.Context%28%29%20is%20passed%20all%20the%20way%20from%20the%20HTTP%20handler%20to%20the%20background%20task%20without%20detachment%2C%20so%20the%20background%20work%20is%20tied%20to%20the%20HTTP%20request%20lifecycle%20and%20gets%20canceled%20too%20soon%C2%A0%5Bsee%20code%5D%28https%3A//github.com/langgenius/dify-plugin-daemon/blob/f272e6c3fcd8a18c6ff571d7fcbfde857f63ef99/internal/service/install_plugin.go%23L35-L142%29.%0A%0AThe%20correct%20fix%20is%20to%20detach%20the%20background%20task%20from%20the%20HTTP%20request%20context.%20This%20was%20addressed%20in%20%5BPR%20%23596%5D%28https%3A//github.com/langgenius/dify-plugin-daemon/pull/596%29%20by%20using%20context.WithoutCancel%28ctx%29%20%28or%20context.Background%28%29%29%20when%20starting%20background%20work%2C%20so%20the%20install/upgrade%20process%20is%20not%20interrupted%20when%20the%20HTTP%20handler%20returns.%20If%20you%20are%20on%20v0.5.3%20or%20earlier%2C%20you%20should%20update%20your%20code%20to%20ensure%20background%20tasks%20do%20not%20use%20the%20request-scoped%20context%20for%20any%20work%20that%20must%20outlive%20the%20HTTP%20request.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify-plugin-daemon/issues/603)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-plugin-daemon#245