spec-first/connexion

How to resolve Connexion.FlaskAPP with ASGI server uvicorn / gunicorn has synchronous api calls Error: ERROR:root:error (0, '')

Closed this issue · 1 comments

Discussed in #1961

Originally posted by karthikc08 August 9, 2024
After migrating from connexion2 to connexion 3 we are facing a weird error causing the application to stop.
When ever multiple api calls are triggered from application to Flask app server with ASGI it randomly throws unauthorized error stating: ERROR:root:error (0, '')

When a single call is made it works but when multiple api calls are triggered from a single promise or one after other application randomly returns result once and throws 401 error some times.

Trace logs:

TRACE:    127.0.0.1:53718 - ASGI [7] Started scope={'type': 'http', 'asgi': {'version': '3.0', 'spec_version': '2.4'}, 'http_version': '1.1', 'server': ('127.0.0.1', 8080), 'client': ('127.0.0.1', 53718), 'scheme': 'http', 'root_path': '', 'headers': '<...>', 'state': {}, 'method': 'GET', 'path': '/api/v1/me/offers', 'raw_path': b'/api/v1/me/offers', 'query_string': b''}
TRACE:    127.0.0.1:53685 - ASGI [6] Send {'type': 'http.response.start', 'status': 401, 'headers': '<...>'}
TRACE:    127.0.0.1:53718 - ASGI [7] Send {'type': 'http.response.start', 'status': 401, 'headers': '<...>'}
TRACE:    127.0.0.1:53685 - ASGI [6] Send {'type': 'http.response.body', 'body': '<90 bytes>', 'more_body': True}
TRACE:    127.0.0.1:53718 - ASGI [7] Send {'type': 'http.response.body', 'body': '<90 bytes>', 'more_body': True}
TRACE:    127.0.0.1:53685 - ASGI [6] Send {'type': 'http.response.body', 'body': '<0 bytes>'}
TRACE:    127.0.0.1:53718 - ASGI [7] Send {'type': 'http.response.body', 'body': '<0 bytes>'}
TRACE:    127.0.0.1:53685 - ASGI [6] Completed
TRACE:    127.0.0.1:53718 - ASGI [7] Completed
ERROR:root:error (0, '')
ERROR:root:error (0, '')
INFO:     127.0.0.1:53685 - "GET /api/v1/me HTTP/1.1" 401 Unauthorized
INFO:     127.0.0.1:53718 - "GET /api/v1/me/offers HTTP/1.1" 401 Unauthorized
TRACE:    127.0.0.1:53685 - HTTP connection lost
TRACE:    127.0.0.1:53718 - HTTP connection lost

Posted the issue on stack overflow too: https://stackoverflow.com/questions/78849875/running-flask-app-with-connexion-3-1-0-with-synchronous-api-calls

Code used:

app = connexion.FlaskApp(__name__, specification_dir='config/swagger/')
CORS(app.app, supports_credentials=True)
app.add_middleware(
      CORSMiddleware,
      position=MiddlewarePosition.BEFORE_EXCEPTION,
      allow_origins=["*"],
      allow_credentials=True,
      allow_methods=["*"],
      allow_headers=["*"],
)

@app.app.after_request
def cache_busting_headers(response):
    response.headers.extend({
        'Last-Modified': datetime.now(),
        'Cache-Control': cache_control_header,
        'Pragma': 'no-cache',
        'Expires': '0'
    })
    return response

def run():
    uvicorn.run(app, port=int(os.getenv('PORT', 8080)), log_level='trace')


if __name__ == '__main__':
    run()

Dependencies used:
connexion[flask,uvicorn,swagger-ui]==3.1.0 Flask-Cors==4.0.1

Can anyone guide me on the solution for this.

Cannot reproduce.