hubcio commented on code in PR #4064:
URL: https://github.com/apache/iggy/pull/4064#discussion_r3942197891


##########
core/connectors/runtime/src/source.rs:
##########
@@ -305,6 +300,63 @@ pub(crate) fn init_source(
     }
 }
 
+/// Closes a source instance that `iggy_source_open` created and nothing else
+/// will ever reach.
+///
+/// Between `init_source` succeeding and the plugin id being recorded on
+/// `SourceDetails`, the instance exists inside the plugin and nothing outside
+/// it knows the id: `stop_connector` closes whatever `details.info.id` holds,
+/// which is still the previous instance. An early return in that window
+/// stranded the new one for the life of the process.
+///
+/// A guard rather than a cleanup branch at the one call site that can fail
+/// today, because the window is defined by the two statements that open and
+/// record the instance, not by which call between them happens to be fallible.
+/// Adding a `?` inside it stays correct.
+pub(crate) struct SourceInstanceGuard<'a> {
+    close: extern "C" fn(u32) -> i32,
+    plugin_id: u32,
+    key: &'a str,
+    armed: bool,
+}
+
+impl<'a> SourceInstanceGuard<'a> {
+    pub(crate) fn new(close: extern "C" fn(u32) -> i32, plugin_id: u32, key: 
&'a str) -> Self {
+        Self {
+            close,
+            plugin_id,
+            key,
+            armed: true,
+        }
+    }
+
+    /// Hands ownership of the instance to the caller, once something else can
+    /// close it. Call only after the plugin id is durably recorded.
+    pub(crate) fn disarm(mut self) {
+        self.armed = false;
+    }
+}
+
+impl Drop for SourceInstanceGuard<'_> {
+    fn drop(&mut self) {
+        if self.armed {
+            close_failed_source(self.close, self.plugin_id, self.key);

Review Comment:
   one more option, if you take the `Arc` suggestion on line 317: move that 
`Arc<Container<SourceApi>>` into a `spawn_blocking` and read 
`iggy_source_close` there. `Container` is `Send + Sync`, so the task keeps the 
library mapped and the close stops parking a worker. tradeoff is the teardown 
becomes unordered against the `Err` return.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to