VirusTotal / vt-py

The official Python 3 client library for VirusTotal
https://virustotal.github.io/vt-py/
Apache License 2.0
532 stars 121 forks source link

Errors Related to Client.__del__ #125

Closed 0xRar closed 1 year ago

0xRar commented 1 year ago

Hey there, my functions are working fine but when i run my script without my arguments i get errors, any idea how to fix it?

Exception ignored in: <function Client.__del__ at 0x0000000003DB9160>
Traceback (most recent call last):
  File "C:\Python39\lib\site-packages\vt\client.py", line 263, in __del__
  File "C:\Python39\lib\site-packages\vt\client.py", line 295, in close
  File "C:\Python39\lib\site-packages\vt\utils.py", line 22, in make_sync
  File "C:\Python39\lib\asyncio\events.py", line 725, in get_event_loop_policy
  File "C:\Python39\lib\asyncio\events.py", line 718, in _init_event_loop_policy
ImportError: sys.meta_path is None, Python is likely shutting down
sys:1: RuntimeWarning: coroutine 'Client.close_async' was never awaited
0xRar commented 1 year ago

I updated & Im still getting errors, i tried with 2 machines now 😥, can you guys test if the problem is fixed for you?

mgmacias95 commented 1 year ago

Hello @0xRar,

127 should fix the problem. Can you test it again?

Thanks!

Regards, Marta

0xRar commented 1 year ago

Hey @mgmacias95,

No, its still there, the errors im getting are kind of different than other people.

mgmacias95 commented 1 year ago

Hello @0xRar,

Can you post a traceback of the exact error you're getting?

Thanks!

Regards, Marta

0xRar commented 1 year ago

@mgmacias95,

Its the same traceback as the one up top but it changed a little because i upgraded to python 3.11,

Exception ignored in: <function Client.__del__ at 0x0000000003A6EB60>
Traceback (most recent call last):
  File "C:\Users\Isa\AppData\Roaming\Python\Python311\site-packages\vt\client.py", line 263, in __del__
  File "C:\Users\Isa\AppData\Roaming\Python\Python311\site-packages\vt\client.py", line 301, in close
  File "C:\Users\Isa\AppData\Roaming\Python\Python311\site-packages\vt\utils.py", line 22, in make_sync
  File "C:\Program Files\Python311\Lib\asyncio\events.py", line 761, in get_event_loop_policy
  File "C:\Program Files\Python311\Lib\asyncio\events.py", line 754, in _init_event_loop_policy
ImportError: sys.meta_path is None, Python is likely shutting down
sys:1: RuntimeWarning: coroutine 'Client.close_async' was never awaited

just a declaimer everything works when using a specific function that interacts with vt-py, so its just a visual error that happens when running the script without using a function that uses vt-py.

Example1: python script.py (will run and show the error) Example2: python script.py -h (will run and show the error) Example3: python script.py --analyze_url https://virustotal.com/ (will run as expected and not show any error at all)

mgmacias95 commented 1 year ago

Testing the following script:

import vt
import asyncio

async def main():
  c = vt.Client('xxx')
  result = await c.scan_url_async('https://google.com/', wait_for_completion=True)
  print(result)
  await c.close_async()

asyncio.run(main())

With a python 3.11 docker image, I was unable to reproduce the error. Can you share some details about the script you're running?

Thanks!

Regards, Marta

NaSangWon commented 1 year ago

I have similar issues. I tried examples/upload_files.py and try to upload two files. and here is a traceback.

Traceback (most recent call last):
  File "C:\Users\yourn\PycharmProjects\Ransomware\ransomware\upload_files.py", line 93, in <module>
    asyncio.run(main())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 649, in run_until_complete
    return future.result()
  File "C:\Users\yourn\PycharmProjects\Ransomware\ransomware\upload_files.py", line 86, in main
    analyses = itertools.chain.from_iterable(await asyncio.gather(*worker_tasks))
  File "C:\Users\yourn\PycharmProjects\Ransomware\ransomware\upload_files.py", line 48, in upload_hashes
    analysis = await client.scan_file_async(file=f, wait_for_completion=True)
  File "C:\Users\yourn\PycharmProjects\Ransomware\venv\lib\site-packages\vt\client.py", line 713, in scan_file_async
    await self._get_session().post(
  File "C:\Users\yourn\PycharmProjects\Ransomware\venv\lib\site-packages\aiohttp\client.py", line 560, in _request
    await resp.start(conn)
  File "C:\Users\yourn\PycharmProjects\Ransomware\venv\lib\site-packages\aiohttp\client_reqrep.py", line 894, in start
    with self._timer:
  File "C:\Users\yourn\PycharmProjects\Ransomware\venv\lib\site-packages\aiohttp\helpers.py", line 720, in __exit__
    raise asyncio.TimeoutError from None
asyncio.exceptions.TimeoutError
Exception ignored in: <function Client.__del__ at 0x000001E424910D30>
Traceback (most recent call last):
  File "C:\Users\yourn\PycharmProjects\Ransomware\venv\lib\site-packages\vt\client.py", line 263, in __del__
  File "C:\Users\yourn\PycharmProjects\Ransomware\venv\lib\site-packages\vt\client.py", line 301, in close
  File "C:\Users\yourn\PycharmProjects\Ransomware\venv\lib\site-packages\vt\utils.py", line 25, in make_sync
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\events.py", line 798, in new_event_loop
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\events.py", line 688, in new_event_loop
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\windows_events.py", line 315, in __init__
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\proactor_events.py", line 639, in __init__
ImportError: sys.meta_path is None, Python is likely shutting down
sys:1: RuntimeWarning: coroutine 'Client.close_async' was never awaited
Exception ignored in: <function Client.__del__ at 0x000001E424910D30>
Traceback (most recent call last):
  File "C:\Users\yourn\PycharmProjects\Ransomware\venv\lib\site-packages\vt\client.py", line 263, in __del__
  File "C:\Users\yourn\PycharmProjects\Ransomware\venv\lib\site-packages\vt\client.py", line 301, in close
  File "C:\Users\yourn\PycharmProjects\Ransomware\venv\lib\site-packages\vt\utils.py", line 25, in make_sync
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\events.py", line 798, in new_event_loop
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\events.py", line 688, in new_event_loop
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\windows_events.py", line 315, in __init__
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2544.0_x64__qbz5n2kfra8p0\lib\asyncio\proactor_events.py", line 639, in __init__
ImportError: sys.meta_path is None, Python is likely shutting down

I did little tweak like you suggested, like,

async def upload_hashes(queue, apikey):
  """Uploads selected files to VirusTotal."""
  return_values = []

  async with vt.Client(apikey) as client:
    while not queue.empty():
      file_path = await queue.get()
      with open(file_path, encoding='utf-8') as f:
        analysis = await client.scan_file_async(file=f, wait_for_completion=True)
        print(f'File {file_path} uploaded.')
        queue.task_done()
        return_values.append((analysis, file_path))
        await client.close_async()

  return return_values
mgmacias95 commented 1 year ago

Hello @NaSangWon,

Please, open a new issue next time. Otherwise it's quite tricky for us to manage (this issue is already closed).

Looking at your code, you're closing the client twice: one by doing await client.close_async() and other when exiting the async with vt.Client block (the context manager closes the client for you).

This is how the code should look:

async def upload_hashes(queue, apikey):
  """Uploads selected files to VirusTotal."""
  return_values = []

  async with vt.Client(apikey) as client:
    while not queue.empty():
      file_path = await queue.get()
      with open(file_path, encoding='utf-8') as f:
        analysis = await client.scan_file_async(file=f, wait_for_completion=True)
        print(f'File {file_path} uploaded.')
        queue.task_done()
        return_values.append((analysis, file_path))

  return return_values

Regards, Marta