fastapi-mqtt icon indicating copy to clipboard operation
fastapi-mqtt copied to clipboard

error Application startup failed. Exiting. when running!

Open hossainirad opened this issue 2 years ago • 8 comments

Hi there. I implemented codes example but I get this error when uvicorn runs:

  File "/home/ali/Documents/first_fastapi/venv/lib/python3.9/site-packages/starlette/routing.py", line 540, in lifespan
    async for item in self.lifespan_context(app):
  File "/home/ali/Documents/first_fastapi/venv/lib/python3.9/site-packages/starlette/routing.py", line 481, in default_lifespan
    await self.startup()
  File "/home/ali/Documents/first_fastapi/venv/lib/python3.9/site-packages/starlette/routing.py", line 516, in startup
    await handler()
  File "/home/ali/Documents/first_fastapi/venv/lib/python3.9/site-packages/fastapi_mqtt/fastmqtt.py", line 276, in startup
    await self.connection()
  File "/home/ali/Documents/first_fastapi/venv/lib/python3.9/site-packages/fastapi_mqtt/fastmqtt.py", line 128, in connection
    await self.client.connect(self.client._host,self.client._port,self.client._ssl,self.client._keepalive,version)
  File "/home/ali/Documents/first_fastapi/venv/lib/python3.9/site-packages/gmqtt/client.py", line 225, in connect
    self._connection = await self._create_connection(
  File "/home/ali/Documents/first_fastapi/venv/lib/python3.9/site-packages/gmqtt/client.py", line 241, in _create_connection
    connection = await MQTTConnection.create_connection(host, port, ssl, clean_session, keepalive)
  File "/home/ali/Documents/first_fastapi/venv/lib/python3.9/site-packages/gmqtt/mqtt/connection.py", line 27, in create_connection
    transport, protocol = await loop.create_connection(MQTTProtocol, host, port, ssl=ssl)
  File "uvloop/loop.pyx", line 2024, in create_connection
  File "uvloop/loop.pyx", line 2001, in uvloop.loop.Loop.create_connection
ConnectionRefusedError: [Errno 111] Connection refused

ERROR:    Application startup failed. Exiting.

hossainirad avatar Aug 23 '21 21:08 hossainirad

Hi, Can you provide a code example?

sabuhish avatar Sep 08 '21 06:09 sabuhish

I came across the same error today

harryliang avatar Sep 16 '21 10:09 harryliang

same here. Looks like with the changes in asyncio, the call to loop doesn't seem to work anymore

IoT-master avatar Sep 17 '21 03:09 IoT-master

how do you guys run applications, could you please provide code examples?

sabuhish avatar Sep 18 '21 09:09 sabuhish

I am experiencing an application startup error as well. Using the code in the example folder and running with: uvicorn app:app --reload

I have tried specifying different hosts and ports when running and in the MQTTConfig. Any updates on this?

howl0893 avatar Apr 18 '22 19:04 howl0893

I have the same bug using code from here. I'm using wsl2 with debian 11, python 3.10.3, fastapi 0.75.2 and running it with command uvicorn main:app

Gipssik avatar Apr 27 '22 23:04 Gipssik

Has this issue been resolved? I've noticed it with all example code provided in the documentation.

run using unvicorn and same startup method as @Gipssik

zabrewer avatar May 09 '22 22:05 zabrewer

Well I don't how you run the application but I will provide the way in which I run the application and perfectly works docker run -it -p 1883:1883 eclipse-mosquitto mosquitto -c /mosquitto-no-auth.conf

Then I use the code example from the README.md file.

with uvicorn uvicorn app:app --port 8002 --reload I run the fastapi app

once I publish the topic /mqtt I can receive the message from broker to the app.

sabuhish avatar Jun 09 '22 10:06 sabuhish

I am also facing the same issue.

irenebaluis avatar Mar 31 '23 14:03 irenebaluis

I will try to check locally again, I know that #25 error happens with gunicorn, but with uvicorn but should not be having issues. please confirm your connection, but with paho-mqtt you can check?

sabuhish avatar Sep 15 '23 12:09 sabuhish

With latest versions, the example apps should work out-of-the-box.

Using a fresh clone and install:

git clone https://github.com/sabuhish/fastapi-mqtt.git
cd fastapi-mqtt
poetry install
poetry shell
uvicorn examples.app:app --port 8000

I'm closing this issue, as completed. Please feel free to re-open it if facing this issue again (and provide details about the used environment 🙏)

azogue avatar Nov 25 '23 17:11 azogue