SwissDataScienceCenter / renku-python

A Python library for the Renku collaborative data science platform.
https://renku-python.readthedocs.io/
Apache License 2.0
37 stars 29 forks source link

Meaningful error when starting a local session with a used port #3383

Open rawlik opened 1 year ago

rawlik commented 1 year ago

At the moment starting a session with a port that is in use $ renku session start --port 8892 throws a monstrous error:

rawlik_m@magnifico ~/r/gict-of-fresh-breast-tissue (master) [1]> renku session start --port 8889                                                                                (base)
Your user id is not 1000 and for Jupyter to work the session must be started as root.
Jupyter itself will run as your user.
Starting as root has security implications, make sure you trust this Dockerfile.
Proceed? [y/N]: y
Error: Docker failed: Docker API returned an error. See inner exception for details.

Traceback (most recent call last):
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/api/client.py", line 268, in _raise_for_status
    response.raise_for_status()
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: http+docker://localhost/v1.41/containers/199a0ff34b321dae11ec38a3d75b1cf195b79375c103f239ab709ab64704a196/start

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/renku/core/session/docker.py", line 243, in session_start_helper
    container = self.docker_client().containers.run(
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/models/containers.py", line 826, in run
    container.start()
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/models/containers.py", line 404, in start
    return self.client.api.start(self.id, **kwargs)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/utils/decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/api/container.py", line 1109, in start
    self._raise_for_status(res)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/api/client.py", line 270, in _raise_for_status
    raise create_api_error_from_http_exception(e)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
    raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 500 Server Error for http+docker://localhost/v1.41/containers/199a0ff34b321dae11ec38a3d75b1cf195b79375c103f239ab709ab64704a196/start: Internal Server Error ("driver failed programming external connectivity on endpoint gifted_cerf (1b0c884aa42f10183b26f6f8502cd33f78fc5a1a446a35d5239923809b1b2d76): Bind for 0.0.0.0:8889 failed: port is already allocated")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/renku/core/session/docker.py", line 280, in session_start
    result = session_start_helper(consider_disk_request=True)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/renku/core/session/docker.py", line 273, in session_start_helper
    raise errors.DockerAPIError("Docker API returned an error. See inner exception for details.") from error
renku.core.errors.DockerAPIError: Docker failed: Docker API returned an error. See inner exception for details.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/api/client.py", line 268, in _raise_for_status
    response.raise_for_status()
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/requests/models.py", line 1021, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 500 Server Error: Internal Server Error for url: http+docker://localhost/v1.41/containers/8f3d27e4912acd5220fb54b9e69abc558b5c76d906fc138f98c3625db957766e/start

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/renku/core/session/docker.py", line 243, in session_start_helper
    container = self.docker_client().containers.run(
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/models/containers.py", line 826, in run
    container.start()
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/models/containers.py", line 404, in start
    return self.client.api.start(self.id, **kwargs)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/utils/decorators.py", line 19, in wrapped
    return f(self, resource_id, *args, **kwargs)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/api/container.py", line 1109, in start
    self._raise_for_status(res)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/api/client.py", line 270, in _raise_for_status
    raise create_api_error_from_http_exception(e)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/docker/errors.py", line 31, in create_api_error_from_http_exception
    raise cls(e, response=response, explanation=explanation)
docker.errors.APIError: 500 Server Error for http+docker://localhost/v1.41/containers/8f3d27e4912acd5220fb54b9e69abc558b5c76d906fc138f98c3625db957766e/start: Internal Server Error ("driver failed programming external connectivity on endpoint loving_rosalind (17aff6a44cd42ed3ccb2e6244bb4d0bf5e9512a72e738a861daed52722a02a85): Bind for 0.0.0.0:8889 failed: port is already allocated")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/renku/ui/cli/exception_handler.py", line 91, in main
    return super().main(*args, **kwargs)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/renku/ui/cli/session.py", line 276, in start
    session_start_command().with_communicator(communicator).build().execute(
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/renku/command/command_builder/command.py", line 264, in execute
    hook(self, context, result, *args, **kwargs)
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/renku/command/command_builder/command.py", line 200, in _post_hook
    raise result.error
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/renku/command/command_builder/command.py", line 250, in execute
    output = self._operation(*args, **kwargs)  # type: ignore
  File "pydantic/decorator.py", line 40, in pydantic.decorator.validate_arguments.validate.wrapper_function
  File "pydantic/decorator.py", line 134, in pydantic.decorator.ValidatedFunction.call
  File "pydantic/decorator.py", line 206, in pydantic.decorator.ValidatedFunction.execute
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/renku/core/session/session.py", line 161, in session_start
    provider_message, warning_message = provider_api.session_start(
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/renku/core/session/docker.py", line 283, in session_start
    return session_start_helper(consider_disk_request=False), warning_message
  File "/home/rawlik_m/.local/pipx/venvs/renku/lib/python3.8/site-packages/renku/core/session/docker.py", line 273, in session_start_helper
    raise errors.DockerAPIError("Docker API returned an error. See inner exception for details.") from error
renku.core.errors.DockerAPIError: Docker failed: Docker API returned an error. See inner exception for details.

A concise message explaining that the port is in use would be better.

Panaetius commented 1 year ago

Thank you for reporting this. We should probably check if a port is available before even attempting to start it.