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

Connection to MQTT broker lost on restart #2073

Closed
nataloko opened this issue May 9, 2024 · 8 comments · Fixed by #2084
Closed

Connection to MQTT broker lost on restart #2073

nataloko opened this issue May 9, 2024 · 8 comments · Fixed by #2084

Comments

@nataloko
Copy link

nataloko commented May 9, 2024

Describe the bug
I have Gladys running in docker and an external MQTT broker (Mosquitto), everything seems to work OK when I enter the address and login details but configuration gets lost when I reboot (or restart Gladys).

To Reproduce

  1. Install Gladys
  2. Go to Integrations -> MQTT
  3. Set-up EXTERNAL broker (screenshot 1)
  4. Restart Gladys
  5. Go to Integrations -> MQTT
  6. See error (screenshot 2)

Expected behavior
I expect the configured broker to stay saved.

Screenshots
image
image

Your Gladys installation (please complete the following information):

  • Raspberry Pi installation: No
  • Manual installation: Docker on Linux.

Desktop (please complete the following information):

  • OS: Linux
  • Browser: Firefox
  • Version: 125.0.03
@nataloko
Copy link
Author

nataloko commented May 9, 2024

I've been playing around a bit more and I think I've found the source:
This bad behaviour only happens when I remove Gladys' connection to Docker socket:
/var/run/docker.sock:/var/run/docker.sock

When I add it back everything works fine again.

In my particular installation I'm handling containers externally so I don't understand why Gladys needs access to the Docker socket?

@nataloko nataloko changed the title MQTT connection with broker keeps breaking Connection to MQTT broker lost on restart May 9, 2024
@Pierre-Gilles
Copy link
Contributor

@nataloko Hey ! Thanks for your bug report.

There might be a bug indeed.

This is the file initializing the MQTT integration => https://github.com/GladysAssistant/Gladys/blob/master/server/services/mqtt/lib/init.js

I've tried on my side, and for now I can't reproduce the bug...

This is my configuration :

{
  mqttUrl: 'mqtt://localhost',
  mqttUsername: null,
  mqttPassword: null,
  useEmbeddedBroker: false,
  dockerBased: false,
  brokerContainerAvailable: false,
  networkModeValid: false,
  mosquittoVersion: null
}

When restarting, MQTT is connected with success

Did you do a "Gladys based installation" before connecting to an external broker?

@nataloko
Copy link
Author

nataloko commented May 18, 2024

Hi Pierre,

I started to see this on a fresh installation.
A difference on my setup is that I've using a user/password to connect to the mqtt broker.

Also I've just checked the locker logs I see this warning when Gladys starts without the docker.sock link (it doesn't show when I add the link back)

2024-05-18T09:21:13+0100 <warn> service.start.js:44 (Service.start) Unable to start service mqtt PlatformNotCompatible [Error]: SYSTEM_NOT_RUNNING_DOCKER

    at System.getNetworkMode (/src/server/lib/system/system.getNetworkMode.js:12:11)

    at MqttHandler.checkDockerNetwork (/src/server/services/mqtt/lib/checkDockerNetwork.js:8:54)

    at MqttHandler.getConfiguration (/src/server/services/mqtt/lib/getConfiguration.js:24:35)

    at MqttHandler.init (/src/server/services/mqtt/lib/init.js:13:25)

    at Object.start (/src/server/services/mqtt/index.js:17:5)

    at Service.start (/src/server/lib/service/service.start.js:33:7)

@nataloko
Copy link
Author

I think that warning could be related; I've just tried with the following with a new Gladys installation:

  1. Create docker compose WITHOUT docker.sock volume
  2. Start container
  3. Do Gladys initial setup
  4. Connect Gladys to mqtt broker - SUCCESS
  5. Restart Gladys container
  6. Go to MQTT integration menu - see "MQTT service is not configured" message
  7. Stop Gladys container
  8. Change docker compose to add docker.sock volume
  9. Start Gladys container
  10. Go to MQTT integration menu - see "Connected to the MQTT broker with success !" message

So it seems that the configuration is actually saved but it's just the connection that is not automatically started when there is no docker.sock.

I've also noticed that after step 4 (initial configuration without docker.sock) if I nagivate away from the "setup" screen and then come back to that screen, the configuration details show blank (although message still says "success").
image

@Pierre-Gilles
Copy link
Contributor

Pierre-Gilles commented May 20, 2024

@nataloko Thanks for your investigation, it helps a lot.

I understand what's going on now, we check in this integration is "Docker based" on this line: https://github.com/GladysAssistant/Gladys/blob/master/server/services/mqtt/lib/getConfiguration.js#L23

If yes, we get the networkMode of the current container, and in your case it fails because the docker socket is not available.

I'm going to add some safety checks to avoid breaking the MQTT integration in case the Docker socket is not here

@Pierre-Gilles
Copy link
Contributor

Fixed in PR : #2084

@Pierre-Gilles
Copy link
Contributor

@nataloko Fix available in Gladys Assistant 4.42

@nataloko
Copy link
Author

Thank you, I can confirm it's working as expected now :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants