Skip to content

Conversation

@IlyaasK
Copy link
Contributor

@IlyaasK IlyaasK commented Dec 22, 2025

Summary

Describe your changes.
Added support for configuring max_connection_idle_time in the Neo4j driver initialization. This allows users to set a maximum idle time for connections via the CLI argument --neo4j-max-connection-idle-time or the environment variable NEO4J_MAX_CONNECTION_IDLE_TIME.
This setting is particularly useful for deployments using Neo4j Aura or other clustered environments, where idle connections may be terminated by the server (often after 3-5 minutes), leading to SessionExpired or ConnectionResetErrors during long syncs.

Related issues or links

Include links to relevant issues or other pages.

Checklist

Provide proof that this works (this makes reviews move faster). Please perform one or more of the following:

  • Update/add unit or integration tests.
  • Include a screenshot showing what the graph looked like before and after your changes.
  • Include console log trace showing what happened before and after your changes.

Verification:
Added a new unit test tests/unit/cartography/test_neo4j_connection.py which mocks the neo4j.GraphDatabase.driver and asserts that the max_connection_idle_time parameter is correctly passed when run_with_config is executed.
If you are changing a node or relationship:

  • Update the schema and readme.
    If you are implementing a new intel module:
  • Use the NodeSchema data model.
  • Confirm that the linter actually passes (submitting a PR where the linter fails shows reviewers that you did not test your code and will delay your review).

Detailed Changes

  • cartography/config.py:
    • Updated Config class to include neo4j_max_connection_idle_time as a documented field.
    • Updated Config.__init__ to accept and store the neo4j_max_connection_idle_time parameter.
  • cartography/cli.py:
    • Added --neo4j-max-connection-idle-time argument to the argparse definition.
    • Added logic to main() to check for the NEO4J_MAX_CONNECTION_IDLE_TIME environment variable if the CLI argument is not provided.
  • cartography/sync.py:
    • Updated run_with_config to pass the max_connection_idle_time parameter to the GraphDatabase.driver initialization.
  • tests/unit/cartography/test_neo4j_connection.py:
    • Added a new unit test to verify that the GraphDatabase.driver is initialized with the correct max_connection_idle_time value when configured.

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 4 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="cartography/cli.py">

<violation number="1" location="cartography/cli.py:997">
P2: Using `not config.neo4j_max_connection_idle_time` will incorrectly allow the environment variable to override an explicit CLI value of `0`. Use `is None` to properly check if the CLI argument was not provided.</violation>
</file>

Reply to cubic to teach it or ask questions. Re-run a review with @cubic-dev-ai review this PR

# TODO support parameter lookup in environment variables if not present on command line
config: argparse.Namespace = self.parser.parse_args(argv)

if not config.neo4j_max_connection_idle_time and os.environ.get(
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Dec 22, 2025

Choose a reason for hiding this comment

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

P2: Using not config.neo4j_max_connection_idle_time will incorrectly allow the environment variable to override an explicit CLI value of 0. Use is None to properly check if the CLI argument was not provided.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At cartography/cli.py, line 997:

<comment>Using `not config.neo4j_max_connection_idle_time` will incorrectly allow the environment variable to override an explicit CLI value of `0`. Use `is None` to properly check if the CLI argument was not provided.</comment>

<file context>
@@ -984,6 +993,14 @@ def main(self, argv: str) -&gt; int:
         # TODO support parameter lookup in environment variables if not present on command line
         config: argparse.Namespace = self.parser.parse_args(argv)
+
+        if not config.neo4j_max_connection_idle_time and os.environ.get(
+            &quot;NEO4J_MAX_CONNECTION_IDLE_TIME&quot;
+        ):
</file context>
Suggested change
if not config.neo4j_max_connection_idle_time and os.environ.get(
if config.neo4j_max_connection_idle_time is None and os.environ.get(

✅ Addressed in bb8f9d4

Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

3 issues found across 2 files (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="tests/unit/cartography/test_neo4j_connection.py">

<violation number="1" location="tests/unit/cartography/test_neo4j_connection.py:23">
P2: Misleading assertion: the test name and config parameter reference &#39;idle_time&#39; but the assertion checks for `max_connection_lifetime`. These are semantically different Neo4j driver parameters. If the intent is to use `max_connection_lifetime`, consider renaming the config parameter and test to reflect this (e.g., `neo4j_max_connection_lifetime`). If `max_connection_idle_time` is the intended parameter, the implementation in sync.py and this assertion should be updated.</violation>
</file>

<file name="cartography/sync.py">

<violation number="1" location="cartography/sync.py:254">
P1: Regression: `neo4j_max_connection_lifetime` config is no longer passed to the driver. The original code passed both `max_connection_lifetime` and `max_connection_idle_time` parameters. Consider also conditionally adding `max_connection_lifetime` from `config.neo4j_max_connection_lifetime` to preserve existing functionality.</violation>

<violation number="2" location="cartography/sync.py:256">
P0: Wrong Neo4j driver parameter name: `max_connection_lifetime` is used instead of `max_connection_idle_time`. The config variable is `neo4j_max_connection_idle_time` and the PR explicitly aims to add support for `max_connection_idle_time`, but this code incorrectly sets `max_connection_lifetime`.</violation>
</file>

Reply to cubic to teach it or ask questions. Re-run a review with @cubic-dev-ai review this PR

# Assert
mock_driver.assert_called_once()
args, kwargs = mock_driver.call_args
assert "max_connection_lifetime" in kwargs
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Dec 22, 2025

Choose a reason for hiding this comment

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

P2: Misleading assertion: the test name and config parameter reference 'idle_time' but the assertion checks for max_connection_lifetime. These are semantically different Neo4j driver parameters. If the intent is to use max_connection_lifetime, consider renaming the config parameter and test to reflect this (e.g., neo4j_max_connection_lifetime). If max_connection_idle_time is the intended parameter, the implementation in sync.py and this assertion should be updated.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At tests/unit/cartography/test_neo4j_connection.py, line 23:

<comment>Misleading assertion: the test name and config parameter reference &#39;idle_time&#39; but the assertion checks for `max_connection_lifetime`. These are semantically different Neo4j driver parameters. If the intent is to use `max_connection_lifetime`, consider renaming the config parameter and test to reflect this (e.g., `neo4j_max_connection_lifetime`). If `max_connection_idle_time` is the intended parameter, the implementation in sync.py and this assertion should be updated.</comment>

<file context>
@@ -20,5 +20,5 @@ def test_neo4j_driver_init_with_idle_time(mock_driver):
     args, kwargs = mock_driver.call_args
-    assert &quot;max_connection_idle_time&quot; in kwargs
-    assert kwargs[&quot;max_connection_idle_time&quot;] == 120
+    assert &quot;max_connection_lifetime&quot; in kwargs
+    assert kwargs[&quot;max_connection_lifetime&quot;] == 120
</file context>
Fix with Cubic

if config.neo4j_user or config.neo4j_password:
neo4j_auth = (config.neo4j_user, config.neo4j_password)
try:
args = {}
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Dec 22, 2025

Choose a reason for hiding this comment

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

P1: Regression: neo4j_max_connection_lifetime config is no longer passed to the driver. The original code passed both max_connection_lifetime and max_connection_idle_time parameters. Consider also conditionally adding max_connection_lifetime from config.neo4j_max_connection_lifetime to preserve existing functionality.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At cartography/sync.py, line 254:

<comment>Regression: `neo4j_max_connection_lifetime` config is no longer passed to the driver. The original code passed both `max_connection_lifetime` and `max_connection_idle_time` parameters. Consider also conditionally adding `max_connection_lifetime` from `config.neo4j_max_connection_lifetime` to preserve existing functionality.</comment>

<file context>
@@ -251,11 +251,14 @@ def run_with_config(sync: Sync, config: Union[Config, argparse.Namespace]) -&gt; in
     if config.neo4j_user or config.neo4j_password:
         neo4j_auth = (config.neo4j_user, config.neo4j_password)
     try:
+        args = {}
+        if config.neo4j_max_connection_idle_time:
+            args[&quot;max_connection_lifetime&quot;] = config.neo4j_max_connection_idle_time
</file context>

✅ Addressed in 64716d0

try:
args = {}
if config.neo4j_max_connection_idle_time:
args["max_connection_lifetime"] = config.neo4j_max_connection_idle_time
Copy link
Contributor

@cubic-dev-ai cubic-dev-ai bot Dec 22, 2025

Choose a reason for hiding this comment

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

P0: Wrong Neo4j driver parameter name: max_connection_lifetime is used instead of max_connection_idle_time. The config variable is neo4j_max_connection_idle_time and the PR explicitly aims to add support for max_connection_idle_time, but this code incorrectly sets max_connection_lifetime.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At cartography/sync.py, line 256:

<comment>Wrong Neo4j driver parameter name: `max_connection_lifetime` is used instead of `max_connection_idle_time`. The config variable is `neo4j_max_connection_idle_time` and the PR explicitly aims to add support for `max_connection_idle_time`, but this code incorrectly sets `max_connection_lifetime`.</comment>

<file context>
@@ -251,11 +251,14 @@ def run_with_config(sync: Sync, config: Union[Config, argparse.Namespace]) -&gt; in
     try:
+        args = {}
+        if config.neo4j_max_connection_idle_time:
+            args[&quot;max_connection_lifetime&quot;] = config.neo4j_max_connection_idle_time
+
         neo4j_driver = GraphDatabase.driver(
</file context>
Suggested change
args["max_connection_lifetime"] = config.neo4j_max_connection_idle_time
args["max_connection_idle_time"] = config.neo4j_max_connection_idle_time

✅ Addressed in 64716d0

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.

Support configuring max_connection_idle_time in Neo4j driver

1 participant