Skip to content

Commit da62a6b

Browse files
committed
fix: pooled processor owns its InferenceConfig instead of referencing it (#76)
BackendBase::m_inference_config was an InferenceConfig& bound to the config of the session that first created the processor. Because processors are pooled and shared between sessions with equal configs (the default), a pooled processor can outlive that session. Each session's InferenceConfig is host-owned, so releasing the originating session frees the config while the pooled processor (still used by a peer session) keeps referencing it — a use-after-free on the next inference. Make the member an owned value so the processor's config lifetime matches the processor. This fixes all four backends at once: each backend's Instance is constructed with the processor's m_inference_config and lives inside the processor, so the instances' InferenceConfig& now alias the processor-owned copy rather than a session's. The pool keys on operator== (value equality), so sharing behaviour is unchanged. Flips the regression test added in the previous commit from failing to passing.
1 parent d3cd827 commit da62a6b

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

include/anira/backends/BackendBase.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,19 @@ class ANIRA_API BackendBase {
2525
/**
2626
* @brief Constructs a BackendBase with the given inference configuration
2727
*
28-
* Initializes the backend processor with a reference to the inference configuration
28+
* Initializes the backend processor with its own copy of the inference configuration
2929
* that contains all necessary parameters for model loading and processing.
3030
*
31-
* @param inference_config Reference to the inference configuration containing
32-
* model data, tensor shapes, and processing specifications
31+
* The configuration is copied (not referenced) so that the processor's lifetime is
32+
* fully decoupled from the session that created it. Processors are pooled and shared
33+
* between sessions whose configs compare equal (see Context::set_processor); a shared
34+
* processor can outlive the session that first created it. If it held a reference into
35+
* that session's InferenceConfig, releasing the session while another session still
36+
* shares the pooled processor would leave the reference dangling, causing a
37+
* use-after-free on the next inference. Owning a copy removes that lifetime coupling.
38+
*
39+
* @param inference_config Reference to the inference configuration to copy from,
40+
* containing model data, tensor shapes, and processing specifications
3341
*/
3442
BackendBase(InferenceConfig& inference_config);
3543

@@ -76,8 +84,10 @@ class ANIRA_API BackendBase {
7684
std::vector<BufferF>& output,
7785
[[maybe_unused]] std::shared_ptr<SessionElement> session);
7886

79-
InferenceConfig& m_inference_config; ///< Reference to inference configuration containing model
80-
///< and processing parameters
87+
InferenceConfig m_inference_config; ///< Owned copy of the inference configuration containing
88+
///< model and processing parameters. Owned (not a
89+
///< reference) so a pooled processor shared across
90+
///< sessions never outlives the config it reads.
8191
};
8292

8393
} // namespace anira

0 commit comments

Comments
 (0)