Skip to content

Conversation

JelteF
Copy link
Collaborator

@JelteF JelteF commented Mar 13, 2025

Fixes #662

@JelteF JelteF requested a review from Y-- March 13, 2025 10:34
@JelteF
Copy link
Collaborator Author

JelteF commented Mar 13, 2025

Reviewing is easiest if you hide whitespace changes

* otherwise we do not clean up the prepared statement and various
* other DuckDB objects.
*/
CleanupDuckdbScanState(duckdb_scan_state);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The usual way to do that is to use RAII. We could add this trivial struct above in the file:

struct ScopedDuckDBScanState {
  ScopedDuckDBScanState(_state) : state(_state);
  ~ScopedDuckDBScanState() {
     CleanupDuckdbScanState(state);
  }

private:
  DuckdbScanState *state;
}

And then at the beginning of this function

  DuckdbScanState *duckdb_scan_state = (DuckdbScanState *)node;
  ScopedDuckDBScanState(duckdb_scan_state);

Copy link
Collaborator Author

@JelteF JelteF Mar 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that would also clean it up on success right? We don't want to do that, because this function is called many times. If the function completes successfully CleanupDuckdbScanState will already be called in Duckdb_EndCustomScan_Cpp.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated the comment to reflect this, because indeed it's not immediately obvious.

Copy link
Collaborator

@Y-- Y-- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, I should have read more carefully.
We could make the ScopedDuckDBScanState to skip cleanup on success but that would be clunky.

I personally would have:

  • renamed Duckdb_ExecCustomScan_Cpp to Duckdb_ExecCustomScan_Cpp_Unsafe
  • implemented Duckdb_ExecCustomScan_Cpp with only the try/catch+cleanup

IMHO this makes the whole cleanup logic more apparent (and probably lessen the need for the comment). But happy with that version too

@JelteF JelteF merged commit 7705bc7 into main Mar 13, 2025
5 checks passed
@JelteF JelteF deleted the handle-leak-in-custom-scan branch March 13, 2025 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Potential leak of file handles (and memory) in case of query cancel or failure
2 participants