Discord py OSError: [WinError 121] The semaphore timeout period has expired
Asked Answered
L

2

9

So recently ive been receiving the error in the title. Im unsure what is causing the problem, it randomly stopped the bot was working about a week ago fine. Ive done my googling and yet still have not found the right fix for this problem. I have linked down below the pastebin of the full error and the link to the discord bot i am currently using. Ive spoken to the devs of the bot and the people who are currently using it have no problems.It seems to be an issue on my end. If anyone could help me with this fix this would be much greatly appreciated.

https://github.com/yannickgloster/discord-10man

OSError: [WinError 121] The semaphore timeout period has expired

https://pastebin.com/2Hsg03if

Lipsey answered 22/10, 2020 at 1:27 Comment(1)
What's your Windows version? What's your Python version?Manipur
M
1

What is a semaphore?

From https://superuser.com/a/1074583/1698404

A semaphore (and a mutex) is a synchronization object, used to communicate amongst various processes sharing a resource. For example, if you had multiple threads performing division checks for primality, you would want to assign different divisors after each test finishes.

What does the timeout mean?

A thread needs to check the semaphore using a Wait Function to see if the object is free. However, to handle deadlocks should one thread fail to release a semaphore, wait functions can specify a finite time-out, the message you see. If a transfer takes too long, then the semaphore controlling it expires.

I read that some people who wanted to connect to a Postgres database, had the same problem. The reason was, that the port on which Postgres communicate was not open on the VPS on which the database was located.

It could be a possible reason, why the error occurres. In your case the error happens at the following code:

session = aiohttp.ClientSession()
base_url = f'http://{self.bot.bot_IP}:{self.bot.web_server.port}'
response = await session.get(f'{base_url}/map-veto')

Maybe the port is not open on the server you want to communicate with.

Another explanation I found is a bug in Python. Means if it's not the reason above, you should ignore/pass the error.

Manipur answered 5/8, 2022 at 21:23 Comment(0)
F
0

In my case the issue was that I was behind a corporate firewall. To fix this semaphore timeout error so my script would work, I added the proxy argument to session.get like in this example snippet:

import aiohttp

url = 'https://example.com'
proxy_url = 'http://<user>:<pass>@<proxy>:<port>'

async with aiohttp.ClientSession() as session:
    async with session.get(url, proxy=proxy_url)

Within Windows Subsystem for Linux (WSL) using Ubuntu/Debian I had to additionally set the SSL context, otherwise an incorrect ca bundle would be used resulting in a SSL Certificate Verify Failed error. For example:

import aiohttp
import ssl

url = 'https://example.com'
proxy_url = 'http://<user>:<pass>@<proxy>:<port>'
path_to_cafile = '/etc/ssl/certs/ca-certificates.crt'
ssl_ctx = ssl.create_default_context(cafile=path_to_cafile)

async with aiohttp.ClientSession() as session:
    async with session.get(url, proxy=proxy_url, ssl=ssl_ctx)

I posted this answer here as well: https://mcmap.net/q/1320427/-aiohttp-client_exceptions-clientconnectorerror-cannot-connect-to-host-443-ssl-default-the-semaphore-timeout-period-has-expired

Here is a related answer from a different user: https://mcmap.net/q/574163/-using-aiohttp-with-proxy

Flaming answered 9/7 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.