cubewise-code / tm1py

TM1py is a Python package that wraps the TM1 REST API in a simple to use library.
http://tm1py.readthedocs.io/en/latest/
MIT License
187 stars 107 forks source link

TCP KEEPALIVE does not work on MacOS #1072

Closed rclapp closed 5 months ago

rclapp commented 6 months ago

When enabling TCP Keep Alive on MacOS connection fails on line 476


            socket_options = urllib3.connection.HTTPConnection.default_socket_options + [
                (socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1),
                (socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, self.TCP_SOCKET_OPTIONS['TCP_KEEPIDLE']),
                (socket.IPPROTO_TCP, socket.TCP_KEEPINTVL, self.TCP_SOCKET_OPTIONS['TCP_KEEPINTVL']),
                (socket.IPPROTO_TCP, socket.TCP_KEEPCNT, self.TCP_SOCKET_OPTIONS['TCP_KEEPCNT'])]

Based on this documentation some socket options are only valid on windows.

https://docs.python.org/3.7/library/socket.html#:~:text=On%20Windows%2C%20TCP_KEEPIDLE%2C%20TCP_KEEPINTVL%20appear%20if%20run%2Dtime%20Windows%20supports.

macsir commented 6 months ago

Could you describe how you got into this issue? I tested it on MacOS and it worked fine.

rclapp commented 6 months ago

Python 3.10

[tm1srv01]
address=
instance=
database=tm1-testing
application_client_id=
application_client_secret=
user=test-user
ssl=False
async_requests_mode=False
timeout=6000
tcp_keepalive=True
config = configparser.ConfigParser()
config.read(Path(__file__).parent.joinpath('config.ini'))
with TM1Service(**config['tm1srv01']) as tm1:
    print(f"Ready: {tm1.connection.session_id}")
    pass
Traceback (most recent call last):
  File "/Users/rdclapp/Applications/PyCharm Professional.app/Contents/plugins/python/helpers/pydev/pydevd.py", line 1534, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/Users/rdclapp/Applications/PyCharm Professional.app/Contents/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/Users/rdclapp/Library/Application Support/JetBrains/PyCharm2023.3/scratches/tm1-manager.py", line 15, in <module>
    with TM1Service(**config['tm1srv01']) as tm1:
  File "/Users/rdclapp/workspaces/public git/TM1py/TM1py/Services/TM1Service.py", line 66, in __init__
    self._tm1_rest = RestService(**kwargs)
  File "/Users/rdclapp/workspaces/public git/TM1py/TM1py/Services/RestService.py", line 205, in __init__
    self._manage_http_adapter()
  File "/Users/rdclapp/workspaces/public git/TM1py/TM1py/Services/RestService.py", line 476, in _manage_http_adapter
    (socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, self.TCP_SOCKET_OPTIONS['TCP_KEEPIDLE']),
AttributeError: module 'socket' has no attribute 'TCP_KEEPIDLE'. Did you mean: 'TCP_KEEPALIVE'?
macsir commented 6 months ago

My apology. I can reproduce this issue and it works initially after replacing TCP_KEEPALIVE with TCP_KEEPIDLE for MacOS. However, after testing some long-run process in the OpenShift cluster. It is facing 60s closed connection error still even with TCP_KEEPALIVE enabled.

requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

After searching, it might be caused by Route timeout configuration does not work properly in OCP 4, OSD and ROSA To make it simple, I think the most appropriate and reliable way is still to use async_requests_mode parameter going forward in cloud environments for long-run requests. Let me create a PR to remove this tcp_keepalive parameter. Thanks.

rclapp commented 6 months ago

Funny you found that, I was actually testing v12 on OpenShift as a part of these tests. PA support was trying to figure out why timeouts were not being respected. Weird thing, is that even with the proper annotations those errors still show up. I will share that link with IBM PA regardless

MariusWirtz commented 5 months ago

solved with #1077