zeyneloz / onesignal_sdk

A Python wrapper around the OneSignal API
MIT License
51 stars 10 forks source link

httpx ReadTimeout can be raised by client.send_notification skipping OneSignalHTTPError #14

Open ryanbabbly opened 4 years ago

ryanbabbly commented 4 years ago

Hi, we're using onesignal_sdk 2.0.0 with django 3.0 and sending some pushes from celery (v4.4.2) using code that looks something like this:

client = Client(
    app_id=settings.ONESIGNAL_APP_ID, rest_api_key=settings.ONESIGNAL_API_KEY
)

try:
    response = client.send_notification(notification_body)
    logging.info(f'OneSignal response: {response.body}')

except OneSignalHTTPError as e:
    # Errors are HTTP status codes
    if str(e.status_code).startswith('5'):
        logging.warn(f'Retrying one signal because error: {e}')
        raise OneSignalRetryError(e)
    else:
        logging.error(
            f'{e.status_code} from OneSignal API with message {e.message}'
        )

And in sentry we've been getting this exception:

ReadTimeout: The read operation timed out
  File "httpx/_exceptions.py", line 342, in map_exceptions
    yield
  File "httpx/_client.py", line 798, in _send_single_request
    ) = transport.request(
  File "httpcore/_sync/connection_pool.py", line 188, in request
    response = connection.request(
  File "httpcore/_sync/connection.py", line 96, in request
    return self.connection.request(method, url, headers, stream, timeout)
  File "httpcore/_sync/http11.py", line 73, in request
    ) = self._receive_response(timeout)
  File "httpcore/_sync/http11.py", line 130, in _receive_response
    event = self._receive_event(timeout)
  File "httpcore/_sync/http11.py", line 160, in _receive_event
    data = self.socket.read(self.READ_NUM_BYTES, timeout)
  File "httpcore/_backends/sync.py", line 62, in read
    return self.sock.recv(n)
  File "contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "httpcore/_exceptions.py", line 12, in map_exceptions
    raise to_exc(exc) from None
ReadTimeout: The read operation timed out
  File "celery/app/base.py", line 487, in run
    return task._orig_run(*args, **kwargs)
  File "core/tasks.py", line 112, in _dispatch_push_notification
    client.send_notification(notification_body)
  File "onesignal_sdk/client.py", line 429, in send_notification
    return basic_auth_request(**self._kwargs_send_notification(notification_body))
  File "onesignal_sdk/request.py", line 37, in basic_auth_request
    return _handle_response(httpx.request(method, url, **request_kwargs))
  File "httpx/_api.py", line 89, in request
    return client.request(
  File "httpx/_client.py", line 673, in request
    return self.send(
  File "httpx/_client.py", line 703, in send
    response = self._send_handling_redirects(
  File "httpx/_client.py", line 732, in _send_handling_redirects
    response = self._send_handling_auth(
  File "httpx/_client.py", line 769, in _send_handling_auth
    response = self._send_single_request(request, timeout)
  File "httpx/_client.py", line 792, in _send_single_request
    (
  File "contextlib.py", line 131, in __exit__
    self.gen.throw(type, value, traceback)
  File "httpx/_exceptions.py", line 359, in map_exceptions
    raise mapped_exc(message, **kwargs) from exc  # type: ignore

For now I'm going to try to catch the exception directly from httpx, or putting a retry on all exceptions, but this seems like a bug or something I'm misunderstanding in my setup.

Please advise how to handle this error, thank you.

ryanbabbly commented 3 years ago

This is still an issue and it has proven difficult to figure out exactly what exception to catch here. Is anyone seeing this and able to advise?

AlexProber commented 3 years ago

I am having the same issue and still not finding a solution - did anyone solve this?

mikelytvynenko commented 2 years ago

Same thing, guys. Same questions...

ahmedesmail07 commented 1 year ago

@ryanbabbly did you find any solution for this? i got the same error here.