Skip to content

Windows socket connection delay #106130

Open
@stacy-rendall

Description

@stacy-rendall

Bug report

Context

We have a pair of AWS Lambda functions that are doing some URL path and header manipulation as part of an AWS API Gateway setup.

I was attempting to simply replicate the pair of functions within a local server so we could do simple local dev/test/debug of the lamba scripts without AWS getting in the way, by creating a FastAPI service hosting the two functions and structuring/destructuring to abstract the AWS bits.

The flow is: request hits Proxy lamba, which calls API lambda, which calls the server, then the response flows back via API to proxy and back to client.

Bug description

When calling the Proxy lambda, which is the normal flow, a two second delay appears in the Proxy lambda call to the API lambda, so the entire call is always 2 seconds plus a handful of milliseconds. This specific value, which remains constant irrespective of the response payload size, is suspicious.

I've created a minimum working example demonstrating the issue here: https://bitbucket.org/stacyr/mwe-windows-socket-delay/src/main/

My suspicion based upon the failing and successful workarounds below is that socket, which I believe is used by urllib, is introducing some kind of timeout on Windows.

Your environment

  • CPython versions tested on: 3.11.4, 3.9.16
  • Operating system and architecture: Windows 10 Pro 64-bit

Workarounds

I've tested a range of unsuccessful ways to try and circumvent the issue:

  • separate servers:
    • hosting one Lambda on Flask, the other on FastAPI
    • hosting both on completely separate Python installs as well as separate servers
  • use built in urllib rather than urllib3 to call between the lambdas

I finally have identified a couple of workarounds, for which the 2 second delay does not occur

Specify a connect timeout

Either, i.e. urllib.request.urlopen(url, timeout=0.5) or

timeout = urllib3.util.Timeout(connect=0.000001)
http = urllib3.PoolManager(timeout=timeout)

It does not make sense why a short connect timeout would solve the problem - connection timeout should specify how long the request waits to connect, if the upstream is slow. If it can connect in a fraction of a millisecond, why is it delaying for two seconds?

Use WSL

As described in MWE readme, running the server on WSL also does not exhibit the delay.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions