Skip to content

Cursor functionality fix#2911

Draft
aaronvg wants to merge 2 commits intocanaryfrom
cursor/cursor-functionality-fix-939d
Draft

Cursor functionality fix#2911
aaronvg wants to merge 2 commits intocanaryfrom
cursor/cursor-functionality-fix-939d

Conversation

@aaronvg
Copy link
Contributor

@aaronvg aaronvg commented Jan 3, 2026

Pull Request Template

Thanks for taking the time to fill out this pull request!

Issue Reference

Please link to any related issues

Changes

Please describe the changes proposed in this pull request

This PR addresses a goroutine leak in the Go BAML client when context.Background() is used for function calls. Previously, a goroutine was spawned to monitor ctx.Done() for cancellation. If context.Background() was passed, this context would never complete, causing the monitoring goroutine to leak indefinitely even after the function call finished.

The fix introduces a done channel for CallFunction, CallFunctionStream, and CallFunctionParse. This channel is closed when the function or stream completes normally. The monitoring goroutine now uses a select statement to listen on both ctx.Done() and the new done channel, ensuring it exits cleanly upon either cancellation or normal function completion.

Testing

Please describe how you tested these changes

  • Unit tests added/updated
  • Manual testing performed (verified that the goroutines exit as expected)
  • Tested in [local development environment]
  • Verified Go code compiles (go build ./...)
  • Ran existing Go tests (go test ./...)

Screenshots

If applicable, add screenshots to help explain your changes

[Add screenshots here...]

PR Checklist

Please ensure you've completed these items

  • I have read and followed the contributing guidelines
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings

Additional Notes

Add any other context about the PR here

The core problem was that context.Background() never signals Done(), leading to leaked goroutines. The done channel provides an alternative signal for normal completion, ensuring proper resource cleanup.


Slack Thread

Open in Cursor Open in Web


Note

Fix goroutine leak in Go runtime

  • Add done channels to CallFunction, CallFunctionStream, and CallFunctionParse so the context-monitor goroutines exit on normal completion or ctx.Done()
  • In CallFunctionStream, close done on error/result and wrap the callback channel to ensure done is closed when the stream ends

Written by Cursor Bugbot for commit d0f1f50. This will update automatically on new commits. Configure here.

This change introduces a `done` channel to gracefully shut down context monitoring goroutines when function calls or streams complete, preventing potential deadlocks.

Co-authored-by: aaron <aaron@boundaryml.com>
@cursor
Copy link

cursor bot commented Jan 3, 2026

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@vercel
Copy link

vercel bot commented Jan 3, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Review Updated (UTC)
promptfiddle Skipped Skipped Jan 3, 2026 7:08am

for res := range callback {
wrappedCallback <- res
}
}()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapper goroutine can leak on context cancellation

The wrapper goroutine in CallFunctionStream is not context-aware. When context is cancelled, if the consumer stops reading from wrappedCallback (a common pattern), the goroutine blocks forever on wrappedCallback <- res. The PR fixes one goroutine leak but introduces a new one. The send to wrappedCallback needs to use a select that also checks ctx.Done() to allow the goroutine to exit when context is cancelled.

Fix in Cursor Fix in Web

This change passes the context object to BAML client functions, which is necessary for proper tracing and cancellation. It also adds a WithClient option to the call options, allowing for more flexible client configuration.

Co-authored-by: aaron <aaron@boundaryml.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Go: CallFunction... BAML runtime methods leak goroutines

2 participants