neo4j/neo4j-python-driver

Exception ignored in: <function Driver.__del__ at 0x11b9fd4c0> when using click

javixeneize opened this issue · 3 comments

Hi

I am having this exception thrown when i use click with neo4j

Exception ignored in: <function Driver.del at 0x11b9fd4c0>
Traceback (most recent call last):
File "/opt/homebrew/lib/python3.9/site-packages/neo4j/_sync/driver.py", line 485, in del
File "/opt/homebrew/lib/python3.9/site-packages/neo4j/_meta.py", line 226, in unclosed_resource_warn
TypeError: 'NoneType' object is not callable

My code is as follows:

if __name__ == "__main__":
    driver = GraphDatabase.driver(os.environ.get('NEO4J_DB'),
                                  auth=(os.environ.get('NEO4J_USER'), os.environ.get('NEO4J_PWD')))
    driver.session()
    run_cli_scan(['test', 'test.json'])
    driver.close()

run_cli_scan is a method that ingests the data in neo4j, as this:

@click.command()
@click.argument('project', required=True)
@click.argument('file', required=True)
def run_cli_scan(project, file):
         result = driver.execute_query('''
    MERGE (n:project {project_name: $project, timestamp: $timestamp})
    RETURN n
    ''', project=project.get('name'), timestamp=project.get('timestamp'))

However if i use sysargv instead of click:

if __name__ == "__main__":
    driver = GraphDatabase.driver(os.environ.get('NEO4J_DB'),
                                  auth=(os.environ.get('NEO4J_USER'), os.environ.get('NEO4J_PWD')))
    try:
        project = sys.argv[1]
        file = sys.argv[2]
        run_cli_scan(project, file)
    except IndexError:
        print ("Arguments missed")
    driver.close()

and i change my run_cli_scan to this:

def run_cli_scan(project, file):
         result = driver.execute_query('''
    MERGE (n:project {project_name: $project, timestamp: $timestamp})
    RETURN n
    ''', project=project.get('name'), timestamp=project.get('timestamp'))

It works fine

Is there any known incompatibility with click? This behaviour is quite weird to me

Thanks

Hi and thanks for reaching out.

What driver version are you using?

As a first a step, I suggest you use the driver and sessions as context manager:

with GraphDatabase.driver(...) as driver:
    with driver.session(...) as session:
        ...  # do things with the session
    # or if you don't need a session, skip the two lines above
    driver.execute_query(...)

Furthermore, it would really be helpful if you could create a single snipped of Python code that runs and shows the issue as I was unable to reproduce it. I couldn't quite string together your snippets to something that executes without modifications.

Hi

it happens with click 3.1.0. Once I use the lates click version it works fine

the code is here https://github.com/javixeneize/neo4cyclone/blob/main/ingest_data_neo4j.py