* libbabeltrace2: question about thread safety
@ 2026-10-23 8:11 MOESSBAUER, Felix via lttng-dev
2026-11-05 19:48 ` Simon Marchi via lttng-dev
[not found] ` <4b159ade-97af-4008-be23-c81be291f948@simark.ca>
0 siblings, 2 replies; 3+ messages in thread
From: MOESSBAUER, Felix via lttng-dev @ 2026-10-23 8:11 UTC (permalink / raw)
To: lttng-dev
Hi,
I just noticed that the reference counting in libbabeltrace2 is not
atomic - and by that not thread safe. As I can't know where reference
counting on global objects (like the plugin or component instances) is
done internally, I'm assuming I can't execute two (independent) graphs
in parallel. Some quick tests with tsan seem to confirm my assumption.
Is there any (documented) way to run two graphs in parallel? Or shall
the whole library just be used behind a global mutex?
Best regards,
Felix
--
Siemens AG
Linux Expert Center
Friedrich-Ludwig-Bauer-Str. 3
85748 Garching, Germany
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: libbabeltrace2: question about thread safety 2026-10-23 8:11 libbabeltrace2: question about thread safety MOESSBAUER, Felix via lttng-dev @ 2026-11-05 19:48 ` Simon Marchi via lttng-dev [not found] ` <4b159ade-97af-4008-be23-c81be291f948@simark.ca> 1 sibling, 0 replies; 3+ messages in thread From: Simon Marchi via lttng-dev @ 2026-11-05 19:48 UTC (permalink / raw) To: MOESSBAUER, Felix, lttng-dev On 10/23/26 4:11 AM, MOESSBAUER, Felix via lttng-dev wrote: > Hi, > > I just noticed that the reference counting in libbabeltrace2 is not > atomic - and by that not thread safe. As I can't know where reference > counting on global objects (like the plugin or component instances) is > done internally, I'm assuming I can't execute two (independent) graphs > in parallel. Some quick tests with tsan seem to confirm my assumption. > > Is there any (documented) way to run two graphs in parallel? Or shall > the whole library just be used behind a global mutex? (resending from another address to try to avoid the spam filter) I think the idea was that running two graphs simultaneously in two threads would not be a problem, because they don't share anything. But you are right that plugins and component classes are actually shared. The action of adding a component to a graph touches the component class' refcount, and so does destroying the graph (and there is the "freeze" status too, if the lib is built in dev mode). I think it would make sense for libbabeltrace2 to have some synchronization in the plugin / component class area to allow for two threads to build, run and destroy graphs without requiring any locking on the user side. But that won't be trivial, and it's not on the roadmap as we speak. In the mean time, you could try having a global lock but only for the graph building and destruction phases? Or have a single thread responsible for building and destroying graphs. I'd be curious to know if this is enough or if there are other concurrent accesses we haven't spotted. Simon ^ permalink raw reply [flat|nested] 3+ messages in thread
[parent not found: <4b159ade-97af-4008-be23-c81be291f948@simark.ca>]
* Re: libbabeltrace2: question about thread safety [not found] ` <4b159ade-97af-4008-be23-c81be291f948@simark.ca> @ 2026-11-06 8:57 ` Moessbauer, Felix via lttng-dev 0 siblings, 0 replies; 3+ messages in thread From: Moessbauer, Felix via lttng-dev @ 2026-11-06 8:57 UTC (permalink / raw) To: Simon Marchi, lttng-dev On Thu, 2026-11-05 at 14:47 -0500, Simon Marchi wrote: > On 10/23/26 4:11 AM, MOESSBAUER, Felix via lttng-dev wrote: > > Hi, > > > > I just noticed that the reference counting in libbabeltrace2 is not > > atomic - and by that not thread safe. As I can't know where reference > > counting on global objects (like the plugin or component instances) is > > done internally, I'm assuming I can't execute two (independent) graphs > > in parallel. Some quick tests with tsan seem to confirm my assumption. > > > > Is there any (documented) way to run two graphs in parallel? Or shall > > the whole library just be used behind a global mutex? > > I think the idea was that running two graphs simultaneously in two > threads would not be a problem, because they don't share anything. But > you are right that plugins and component classes are actually shared. > The action of adding a component to a graph touches the component class' > refcount, and so does destroying the graph (and there is the "freeze" > status too, if the lib is built in dev mode). Hi, thanks for the clarification. This behavior should at least be documented. > > I think it would make sense for libbabeltrace2 to have some > synchronization in the plugin / component class area to allow for two > threads to build, run and destroy graphs without requiring any locking > on the user side. But that won't be trivial, and it's not on the > roadmap as we speak. Without looking into it more deeply, I guess just making the ref counting atomic would be sufficient. That should be a rather small change with a minimal performance impact on modern platforms. > > In the mean time, you could try having a global lock but only for the > graph building and destruction phases? Or have a single thread > responsible for building and destroying graphs. I'd be curious to know > if this is enough or if there are other concurrent accesses we haven't > spotted. As I was unsure what exactly is shared, I just moved the graph processing to individual processes (the processing is done as part of an HTTP MCP server, so I expect multiple calls to arrive in parallel). PS: While this solves the race conditions on the refcounting, it does not help to speedup the graph processing, as this still happens sequentially. Ideally that could be parallelized as well, especially as we know the graph layout upfront. Felix > > Simon ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-06 8:57 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-23 8:11 libbabeltrace2: question about thread safety MOESSBAUER, Felix via lttng-dev
2026-11-05 19:48 ` Simon Marchi via lttng-dev
[not found] ` <4b159ade-97af-4008-be23-c81be291f948@simark.ca>
2026-11-06 8:57 ` Moessbauer, Felix via lttng-dev
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox