Skip to content

Fix some memory Leak about transactions, loggers, strdups#25

Merged
johannessen merged 4 commits intomajensen:mainfrom
npc1054657282:fix/transaction-memory-leak
Aug 2, 2025
Merged

Fix some memory Leak about transactions, loggers, strdups#25
johannessen merged 4 commits intomajensen:mainfrom
npc1054657282:fix/transaction-memory-leak

Conversation

@npc1054657282
Copy link
Copy Markdown
Contributor

@npc1054657282 npc1054657282 commented Jul 3, 2025

  1. In new_transaction, the resources it allocated were all not released in neo4j_free_tx, including the unreleased logger, undrained mpool and unfree strdup.
  2. The strdup issue in new_transaction is more complicated, because the ownership of the memory for tx->mode is very ambiguous. I try to change this to be only static "w" or static "r". If the input mode is not equal to "r", then tx->mode will be default "w".
  3. The tx->dbname in transaction is also a problem. If it would free, the dbname must be defined as char * rather than const char *. Considering the transaction owns the dbanme, it should hold the string to be not const since the transaction should take responsibility to free it.
  4. Another unfree strdup is found in neo4j_config_set_supported_versions.
  5. In neo4j_check_known_hosts, the REQUIRED macro add a concealed return point without the neo4j_logger_release. The macro must be used at the beginning of a function.
  6. The std logger provider is not multithread safe. I think it is easy to be misused, so I add some documentations.

In `new_transaction`, a logger is created by `neo4j_get_logger`.
The logger get by this way must be released by `neo4j_logger_release`.
Unreleased logger resulted in a memory leak here.
The memory allocated in the transaction mpool will leak without drained.
In `neo4j_config_set_supported_versions` and `new_transaction`, the `strdup`ed vals never free.
The especially special one is `tx->mode = ( mode == NULL ? "w" : strdup(mode) )`
The ownership of the memory for `tx->mode` is very ambiguous. It's a headache to release it.
So I change this to be only static "w" or static "s".
If the input `mode` is not equal to "s", then `tx->mode` will be default "w".
The `tx->dbname` also has an issue. Though it is defined as `const char *`, which means the tx does not own the string object.
However, it is created with `strdup`, which means it owns the string and should free it. So I believe its type is incorrect.
An alternative option is to let the dbname just refer the arg `dbname`, which require the caller to ensure the lifetime of the `dbname`.
I do not like this. I choose to modify the `tx->mode` to `char *`.
The `neo4j_check_known_hosts` used the `REQUIRE` macro after the `neo4j_get_logger`,
resulting in a memory since the `REQUIRE` add a concealed return point without the
`neo4j_logger_release`. The `REQUIRE` macro must be used at the beginning of a function.
Meanwhile, I find that the `neo4j_std_logger_provider` is not multithread safe.
This maintain the linked list and ref count without atomic or lock, which would be very
prone to being misused. I think the `neo4j_std_logger_provider` is not intended to be used
under a multithread scene, so I add some docs to warn user who may want to share a std
logger provider among connections in different thread.
@npc1054657282 npc1054657282 changed the title Fix ome memory Leak about transactions, loggers, strdups Fix some memory Leak about transactions, loggers, strdups Jul 3, 2025
Copy link
Copy Markdown
Collaborator

@johannessen johannessen left a comment

Choose a reason for hiding this comment

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

Thanks. Good call on tx->mode and tx->dbname.

@johannessen johannessen merged commit e3f4ed9 into majensen:main Aug 2, 2025
2 checks passed
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.

2 participants