https://www.boundaryml.com logo
🟑 BAML Segfault Errors in Version 0.206.1
# questions
h
Hi team, I am getting baml segfault errors in
0.206.1
Copy code
called "Result::unwrap()" on an "Err" value: PoisonError { .. }
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::result::unwrap_failed
   3: baml_runtime::tracing::BamlTracer::start_call
   4: baml_runtime::BamlRuntime::call_function_with_expr_events::{{closure}}
   5: <pyo3_async_runtimes::generic::Cancellable<F> as core::future::future::Future>::poll
   6: <tokio::task::task_local::TaskLocalFuture<T,F> as core::future::future::Future>::poll{{reify.shim}}
   7: tokio::runtime::task::core::Core<T,S>::poll
   8: tokio::runtime::task::raw::poll
   9: tokio::runtime::scheduler::multi_thread::worker::Context::run_task
  10: tokio::runtime::task::raw::poll
note: Some details are omitted, run with "RUST_BACKTRACE=full" for a verbose backtrace.
thread "<unnamed>" panicked at baml-runtime/src/tracingv2/storage/storage.rs:669:45:
I do not see any fix in the storage.rs between 0.206.1 and 0.211.2. Will bump the version to latest and see if this is still an issue but sharing to let you know in the meantime.
v
Thanks for letting me know! What language are you using?
h
this is Python
3.12
v
ok great thanks for reprting. and can you describe what kind of behaiour you're doing, e.g. calling a bunch of stuff in parallel? celery tasks? threading etc?
(We'll try and repo ourselves)
Doing 20 concurrents call to GEMINI_2_5_FLASH_LITE
20 concurrents calls over 1_000 calls and getting the logs from a baml collector
v
perfect awesome. I'll repro this today ASAP. can you share your code snippet btw as well?
h
I can share pseudo code but not the code sorry as it contains proprietary code
v
psuedo code would be great! would just help me repro this faster (patching another bug asap)
h
Copy code
python
import asyncio
from collections.abc import Generator
from contextlib import contextmanager
from typing import TYPE_CHECKING, Literal


Components = Literal["agent"]

if TYPE_CHECKING:
    from baml_py import Collector


@contextmanager
def baml_instrumentation(caller: Components) -> Generator["Collector"]:
    from baml_py import Collector

    baml_collector = Collector(caller)
    try:
        yield baml_collector
    finally:
        _record_baml_usage(baml_collector)


def get_usage_bytes_from_baml_collector(baml_collector: "Collector") -> int:
    total_bytes = 0
    for log in baml_collector.logs:
        for call in log.calls:
            if call.http_request:
                total_bytes += len(call.http_request.body.text().encode("utf-8"))
            if call.http_response:
                total_bytes += len(call.http_response.body.text().encode("utf-8"))
    return total_bytes


def _call_llm(baml_collector: "Collector", semaphore: asyncio.Semaphore):
    from baml_client.async_client import BamlCallOptions
    from baml_client import async_client
    # Set up client_registry with GEMINI 2.5

    baml_options = BamlCallOptions(
        client_registry=client_registry,
        collector=baml_collector,
    )

    return await async_client.b.GenerateText()


async def main():
    semaphore = asyncio.BoundedSemaphore(20)
    with baml_instrumentation("agent") as baml_collector:
        async with asyncio.TaskGroup() as tg:
            tasks = [
                tg.create_task(
                    _call_llm(
                        baml_collector=baml_collector,
                        semaphore=semaphore,
                    )
                )
                for i in range(1_000)
            ]

        total_bytes_processed = get_usage_bytes_from_baml_collector(baml_collector)
Let me know if that works, I added the typings as well to make it easier for you
v
this is beautiful! thanks so much @hunterlige
h
Done the bumping to 0.211.2 as well, will try to reproduce on my side
v
@aaronv (BAML)
h
Any update on this? Is this part of 213?
a
I will send you a way to get rid of this issue as a workaround! ETA is tomorrow. Apologies for the delay
The key is to use the β€œtags” field in the baml function call to send an id, and collect the logs that have tags with that id
Without using .last
h
Awesome, thank you
a
what does _record_baml_usage do in your app?
instead of using .last
h
@aaronv (BAML) Thank you a lot, will use this check to make sure that the log is populated before accessing it. Just wondering, is a fix plan to be implemented in BAML in future version.
a
we would need to ensure this particular code is threadsafe, perhaps using python contexts. We don't have a timeline for it at the moment -- you would just need to make sure you dont try to access the logs while the batch is still running (or use the id mechanism i mentioned)