Skip to content

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

@invakid404

Description

@invakid404

Product

BAML

Describe the bug

The generated parse and parse_stream methods in the Golang client call bamlRuntime.CallFunctionParse with a context.Background(), and CallFunctionParse unconditionally spawns a goroutine that waits for the context to be done (link), but since nothing else can make that goroutine stop, and context.Background() will never complete, the goroutine leaks.

This pattern of "wait for context to be done" goroutines appears in bamlRuntime.CallFunction as well, but at least there the context is user-controlled, so the goroutine leak is "avoidable", but definitely still realistic, especially considering the fact that BAML docs encourage the usage of context.Background() through the official examples.

Reproduction Steps

  1. Set up a Go BAML project with some goroutine profiling mechanism like pprof
  2. Run some BAML functions
  3. Observe the multiple leaked goroutines via your profiling mechanism of choice, e.g.:
Image

BAML Version

Identified on 0.214.0, evidently present in latest as well judging by the code

Language/Framework

Go

LLM Provider

None

LLM Model

No response

Operating System

None

Browser

None

Code Editor

None

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions