Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot capture the exception in case the server port is already occupied #1963

Open
1 of 3 tasks
rkachach opened this issue May 27, 2022 · 0 comments
Open
1 of 3 tasks

Comments

@rkachach
Copy link

rkachach commented May 27, 2022

I'm submitting a ...

  • bug report
  • feature request
  • question about the decisions made in the repository

Do you want to request a feature or report a bug?
BUG:

[27/May/2022:15:26:08] ENGINE Bus STARTING
[27/May/2022:15:26:08] ENGINE Started monitor thread 'Autoreloader'.
[27/May/2022:15:26:09] ENGINE Error in 'start' listener <bound method Server.start of <cherrypy._cpserver.Server object at 0x7f731a28ea40>>
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/portend.py", line 122, in free
    Checker(timeout=0.1).assert_free(host, port)
  File "/usr/lib/python3.10/site-packages/portend.py", line 71, in assert_free
    list(itertools.starmap(self._connect, info))
  File "/usr/lib/python3.10/site-packages/portend.py", line 87, in _connect
    raise PortNotFree(tmpl.format(**locals()))
portend.PortNotFree: Port 2000 is in use on 192.168.100.1.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/cherrypy/process/wspbus.py", line 230, in publish
    output.append(listener(*args, **kwargs))
  File "/usr/lib/python3.10/site-packages/cherrypy/_cpserver.py", line 180, in start
    super(Server, self).start()
  File "/usr/lib/python3.10/site-packages/cherrypy/process/servers.py", line 177, in start
    portend.free(*self.bind_addr, timeout=Timeouts.free)
  File "/usr/lib/python3.10/site-packages/portend.py", line 126, in free
    raise Timeout("Port {port} not free on {host}.".format(**locals()))
portend.Timeout: Port 2000 not free on 192.168.100.1.

[27/May/2022:15:26:09] ENGINE Shutting down due to error in start listener:
Traceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/cherrypy/process/wspbus.py", line 268, in start
    self.publish('start')
  File "/usr/lib/python3.10/site-packages/cherrypy/process/wspbus.py", line 248, in publish
    raise exc
cherrypy.process.wspbus.ChannelFailures: Timeout('Port 2000 not free on 192.168.100.1.')

[27/May/2022:15:26:09] ENGINE Bus STOPPING
[27/May/2022:15:26:09] ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(('192.168.100.1', 2000)) already shut down
[27/May/2022:15:26:09] ENGINE Stopped thread 'Autoreloader'.
[27/May/2022:15:26:09] ENGINE Bus STOPPED
[27/May/2022:15:26:09] ENGINE Bus EXITING
[27/May/2022:15:26:09] ENGINE Bus EXITED

What is the current behavior?
It's not possible to capture the exception when trying to bind the server in a port which is already occupied

If the current behavior is a bug, please provide the steps to reproduce and if possible a screenshots and logs of the problem. If you can, show us your code.

Run the following code twice: the first one to reserver the port (2000 in this case) and in the second one the code will fail to capture the exception.

import cherrypy

class App1(object):
    @cherrypy.expose
    def index(self):
         return ('server1')

cherrypy.server.unsubscribe()

server2 = cherrypy._cpserver.Server()
server2.socket_port=2000
server2._socket_host="192.168.100.1"
server2.thread_pool=2
server2.subscribe()

try:
    cherrypy.engine.start()
    cherrypy.engine.block()
except Exception as e:
    print(f"Exception: {e}")

What is the expected behavior?
Be able to capture the exception

What is the motivation / use case for changing the behavior?
Detect the case when a port is already in use and try another one.

Please tell us about your environment:

  • Cheroot version: 8.5.2
  • CherryPy version: 18.6.1
  • Python version: 3.6.X
  • OS: Fedora 35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant