Skip to content

fix: isolate backwards invocation contexts - #788

Merged
fatelei merged 1 commit into
langgenius:mainfrom
euxx:fix/isolate-backwards-invocation-context
Aug 4, 2026
Merged

fix: isolate backwards invocation contexts#788
fatelei merged 1 commit into
langgenius:mainfrom
euxx:fix/isolate-backwards-invocation-context

Conversation

@euxx

@euxx euxx commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Description

PluginManager currently returns one shared BackwardsInvocation. Plugin sessions mutate that instance with their HTTP request context. After a request finishes, its canceled context can therefore leak into later endpoint setup or listing calls, causing /inner/api/invoke/encrypt to fail locally with context canceled before the request reaches Dify API.

Fixes #787

Root cause

Session.propagateTraceContext calls SetContext on the invocation returned by PluginManager. Because the manager returned its stored singleton directly, the session replaced the singleton's context. The canceled request context then remained attached to all later callers.

Changes

  • adds WithContext to create a context-bound invocation copy while reusing the HTTP client and immutable configuration
  • returns a fresh invocation from PluginManager.BackwardsInvocation()
  • preserves per-session cancellation by propagating the active request context only to that session's copy
  • updates the mock implementation and adds regression tests for both the concrete invocation and manager behavior

Type of Change

  • Bug fix
  • New feature
  • Refactor
  • Performance improvement
  • Other

Essential Checklist

Testing

  • I have tested the changes locally and confirmed they work as expected
  • I have added unit tests where necessary and they pass successfully

Bug Fix (if applicable)

  • I have used GitHub syntax to close the related issue (e.g., Fixes #123 or Closes #123)

Additional Information

Validation performed locally:

go run cmd/license/generate/main.go
go test -v -timeout 1m ./cmd/... ./internal/... ./pkg/...
go test -race ./internal/core/plugin_manager ./internal/core/dify_invocation/calldify ./internal/core/session_manager

The full unit test suite and focused race tests pass. A control test on the unmodified main branch reproduces the same context canceled error, while the patched branch completes the request successfully.

Return a context-bound invocation copy for each manager request so canceled plugin sessions cannot affect subsequent calls to Dify internal APIs.
@dosubot dosubot Bot added size:M This PR changes 30-99 lines, ignoring generated files. bug Something isn't working go Pull requests that update go code labels Jul 31, 2026
@dosubot dosubot Bot added the lgtm This PR has been approved by a maintainer label Aug 4, 2026
@fatelei
fatelei merged commit 76ddb62 into langgenius:main Aug 4, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update go code lgtm This PR has been approved by a maintainer size:M This PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Canceled plugin session context leaks into endpoint encryption requests

2 participants