GreenDelta / olca-ipc.py

Mozilla Public License 2.0
29 stars 17 forks source link

OS Error: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted #14

Closed ezrakahn closed 1 month ago

ezrakahn commented 3 years ago

Traceback (most recent call last): File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\connection.py", line 160, in _new_conn (self._dns_host, self.port), self.timeout, **extra_kw File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\connection.py", line 84, in create_connection raise err File "C:\ProgramData\Anaconda3\lib\site-packages\urllib3\util\connection.py", line 74, in create_connection sock.connect(sa) OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted

I'm getting this OSError with IPC when i am performing many calls to the client. I am cleaning a database in olca 1.10.3 by deleting unused FedEFL flows, with client.delete(Flow). I will get this error after several thousand successful calls.

Miguel-g-c commented 3 years ago

Hello,

I've been testing it a while and it seems to be a server side problem. When too many requests are done in a brief period of time, server refuses the connection. It is a security setting in the server side, which maybe could be disabled when IPC server is meant to be used locally.

Meanwhile, in the client side, user can catch the error and sleep the execution to avoid this problem:

import unittest
import olca
import uuid
import time
import requests

class TestClientConnection(unittest.TestCase):

    def setUp(self):
        self.client = olca.Client(8080)

        self.test_flow = olca.Flow()
        self.test_flow.id = str(uuid.uuid4())
        self.test_flow.flow_type = olca.FlowType.PRODUCT_FLOW
        self.test_flow.name = "Test Flow"
        self.test_flow.description = "Added from the olca-ipc python API..."

        mass = self.client.find(olca.FlowProperty, 'Mass')
        mass_factor = olca.FlowPropertyFactor()
        mass_factor.conversion_factor = 1.0
        mass_factor.flow_property = mass
        mass_factor.reference_flow_property = True
        self.test_flow.flow_properties = [mass_factor]

    def test_multiple_connections(self):
        for i in range(15000):
            with self.subTest(i=i):
                try:
                    self.client.insert(self.test_flow)
                    response = self.client.delete(self.test_flow)
                except requests.exceptions.ConnectionError:
                    print('Letting server rest...')
                    time.sleep(30)
                    self.client.insert(self.test_flow)
                    response = self.client.delete(self.test_flow)

                print(i, response)
                self.assertEqual(response, 'ok')

if __name__ == '__main__':
    unittest.main()

This test is completed without errors.

yvechang commented 2 years ago

Hello @Miguel-g-c, I also need a solution to fixed up the OSError: [WinError 10048]. I saved your code tobe the file main.py and tested by cmd "python main.py." However, I got the following result.


Ran 1 test in 4.080s

FAILED (errors=1)


The error hints are,

Traceback (most recent call last): File "C:\Users\yvechang\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 158, in _new_conn conn = connection.create_connection( File "C:\Users\yvechang\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\connection.py", line 80, in create_connection raise err File "C:\Users\yvechang\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\util\connection.py", line 70, in create_connection sock.connect(sa) ConnectionRefusedError: [WinError 10061] 無法連線,因為目標電腦拒絕連線。

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\yvechang\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 597, in urlopen httplib_response = self._make_request(conn, method, url, File "C:\Users\yvechang\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connectionpool.py", line 354, in _make_request conn.request(method, url, **httplib_request_kw) File "C:\Users\yvechang\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1276, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Users\yvechang\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1322, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users\yvechang\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1271, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Users\yvechang\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 1031, in _send_output self.send(msg) File "C:\Users\yvechang\AppData\Local\Programs\Python\Python310\lib\http\client.py", line 969, in send self.connect() File "C:\Users\yvechang\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 181, in connect conn = self._new_conn() File "C:\Users\yvechang\AppData\Local\Programs\Python\Python310\lib\site-packages\urllib3\connection.py", line 167, in _new_conn raise NewConnectionError( urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x000001609DA22D40>: Failed to establish a new connection: [WinError 10061] 無法連線,因為目標電腦拒絕連線。

Miguel-g-c commented 2 years ago

Hi @yvechang,

Can you translate this: 無法連線,因為目標電腦拒絕連線? Anyways, the server is refusing connections when they are made too fast and in a huge number (to avoid DDoS attacks). So instead of sleeping the execution when a ConnectionError is raised you can sleep a bit the execution between requests. For example:

for i in range(15000):
    self.client.insert(self.test_flow)
    response = self.client.delete(self.test_flow)
    time.sleep(1)
    self.assertEqual(response, 'ok')
yvechang commented 2 years ago

"無法連線,因為目標電腦拒絕連線" means >> No connection could be made because the target machine actively refused it

yvechang commented 2 years ago

Hello @Miguel-g-c, "無法連線,因為目標電腦拒絕連線" means >> No connection could be made because the target machine actively refused it

I had tried your suggestion, but no matter sleep(1), sleep(10) or sleep(100), I got the same fault.

Thank you.

msrocka commented 1 month ago

We switched to request sessions and also use a different server in current openLCA versions now. Maybe this is not an issue anymore, otherwise just reopen this issue.