VirusTotal / vt-py

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

Error when using vt-py from a GCP Cloud Function #183

Closed mbevilacqua closed 3 months ago

mbevilacqua commented 5 months ago

Having issues trying to get vt-py running in a GCP Cloud Function. Some quick googling around lead to a similar-looking bug in the cloud-sql-python-connect

  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/vt/client.py", line 239, in __init__
    self._connector = aiohttp.TCPConnector(ssl=self._verify_ssl)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/aiohttp/connector.py", line 776, in __init__
    super().__init__(
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/aiohttp/connector.py", line 232, in __init__
    loop = get_running_loop(loop)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/aiohttp/helpers.py", line 300, in get_running_loop
    loop = asyncio.get_event_loop()
  File "/layers/google.python.runtime/python/lib/python3.8/asyncio/events.py", line 639, in get_event_loop
    raise RuntimeError('There is no current event loop in thread %r.'
RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'."
mbevilacqua commented 5 months ago

FWIW I did get it to run with the following construct but a more seamless experience for Cloud Functions, if possible, would be appreciated.

    try:
        event_loop = asyncio.get_event_loop()
    except RuntimeError as e:
        event_loop = asyncio.new_event_loop()
        asyncio.set_event_loop(event_loop)
mgmacias95 commented 5 months ago

hello @mbevilacqua,

does the same issue happen with modern versions of python? such as python 3.11. I'm asking since python 3.8 is quite old. It shouldn't be much of a problem to update to a modern version of python.

Thanks!

mbevilacqua commented 5 months ago

Yes, same thing happens with 3.12, I need the extra try/catch to run this as a cloud function.

rrlapointe commented 3 months ago

I am getting the same error when using this library in a WSGI web application. Rolling back to version 0.17.5 fixes the issue. Caused by 87ab6870ddb476c3cc9c854a89caaf8f1cf5374f. Related: #21

mgmacias95 commented 3 months ago

Checked this again using this tester:

import flask
import os
import vt

app = flask.Flask(__name__)

@app.get('/')
def home():
  with vt.Client(os.getenv('VT_APIKEY')) as c:
    g = c.get('/domains/google.com')
    return g.json()

The problem is that the TCPConnector connector expects to be called inside an async coroutine. Thank you for reporting, I'm already working on a fix :)