SSHLibrary icon indicating copy to clipboard operation
SSHLibrary copied to clipboard

Possible race condition when closing tunnel created using `Create Local Ssh Tunnel`

Open mikatammi opened this issue 2 years ago • 2 comments

Steps to reproduce:

Have multiple test cases run after another, which do "Create Local Ssh Tunnel" in setup-phase, and "Close All Connections" in teardown-phase.

Expected results:

Close All Connections would close all the connections, and then the Create Local Ssh Tunnel would simply work.

What really happens:

As in the picture below, the tests start to say OSError: [Errno 98] Address already in use

image

I'm suspecting there should be some kind of wait, eg. t.wait() for the thread after calling shutdown here: https://github.com/robotframework/SSHLibrary/blob/master/src/SSHLibrary/pythonforward.py#L44

mikatammi avatar Nov 03 '23 13:11 mikatammi

I'm able to circumvent around the issue by using this keyword which I copy-pasted from the test cases of the SSHLibrary, but I still think it would be nice to fix the actual issue:

Port Should Be Free
    [Arguments]  ${port}
    ${result}  Run Keyword If  os.sep == '/'
    ...  Run  netstat -tulpn
    ...  ELSE
    ...  Run  netstat -an
    Should Not Contain  ${result}  :${port}
    [Return]  ${result}

and then call Wait Until Keyword Succeeds 2 min 10s Port Should Be Free ${local_port} before calling the "Create Local Ssh Tunnel"

https://github.com/tiiuae/ci-test-automation/pull/59/commits/a790633d0420344dff9542599e020bcc5604f7df

mikatammi avatar Nov 03 '23 13:11 mikatammi

From the documentatin of socketserver: "Finally, call server_close() to close the socket (unless you used a with statement).". So I think the line self.server.server_close() should be added after the shutdown to clean things up

mikatammi avatar Nov 03 '23 14:11 mikatammi