0

I'm developing a Flask app to host a REST API service. In order to distribute the code for testing on differing systems, I can't hard-code the port that the application runs on.

When running Flask 0.12.2 I am able to use the command app.run(port=0) to get a usable port assigned by the OS. This works.

When I try the same command with Flask 1.0.2, I receive the message OSError: [Errno 98] Address already in use.

Does anyone know why there is this discrepancy, and whether there's a workaround using Flask 1.0.2?

I'm aware that it's possible to get a socket assigned by the OS using Python's socket module, but the ensuing race condition isn't acceptable for the testing environment.

Ben
  • 21
  • 4
  • try to use a port other than 0. Try using 8888 – harveyslash Sep 28 '18 at 17:40
  • Does 8888 have special properties? I can't hard-code the port, as stated in the question – Ben Sep 28 '18 at 17:42
  • Hard coding in a specific, available port does work. I can't make the assumption that any specific port will be free on all the machines this may run on though – Ben Sep 28 '18 at 17:47
  • Check this post: https://stackoverflow.com/questions/5085656/how-to-get-the-current-port-number-in-flask – sal Sep 28 '18 at 17:47
  • That generates the race condition mentioned in the question. That race condition is not acceptable in the testing environment – Ben Sep 28 '18 at 17:48
  • If the most important thing is for the app to start no matter the port, then you can randomize the port selection and try `app.run(port=)` until it goes through. This way you can handle clashes which apparently can happen in your environment. If there is another constraint, please explain. – sal Sep 28 '18 at 17:53
  • I'll consider the randomized option. I would really like to find out why this feature of Flask seems to have regressed, but that may be a suitable work around. – Ben Sep 28 '18 at 17:56
  • I feel you. It might reside in changes in `werkzeug`, but haven't looked into it. – sal Sep 28 '18 at 18:00

0 Answers0