Coder Social home page Coder Social logo

Comments (7)

JakubSzajna avatar JakubSzajna commented on June 11, 2024 3

Hello!
I just run into that issue too. It's due to a fact that Exception here is in fact MQTT::Exception instead of ::Exception and I wonder if this is intentionally implemented that way? Because of that, if the connection is broken for some reason the thread dies silently and the only trace of that is in the logs.

I don't want to globally set abort_on_exception, but if using MQTT::Exception is intentional, maybe allowing one to set this value for the @read_thread specifically via some config option + having the issue described somewhere would be enough?

My fix for now is something like this:

MQTT::Client.prepend(Module.new do
  def receive_packet
    super
  rescue ::Exception => ex
    Thread.current[:parent].raise(ex)
  end
end)

from ruby-mqtt.

JakubSzajna avatar JakubSzajna commented on June 11, 2024 3

I don't think this fixes the issue were talking about here. If I find some time, I'll create a PR to include a proposed solution in the gems code.

from ruby-mqtt.

phlegx avatar phlegx commented on June 11, 2024

@JakubSzajna but ruby-mqtt has this definition: mqtt.rb#L23

module MQTT
  ...
  # Super-class for other MQTT related exceptions
  class Exception < ::Exception
  end
  ...
end

Used here: mqtt/client.rb#L476

# Pass exceptions up to parent thread
rescue Exception => exp
  unless @socket.nil?
    @socket.close
    @socket = nil
    handle_close
  end
  Thread.current[:parent].raise(exp)
end

Is @socket.close and handle_close considered in your fix? How can I catch the exception in my case, described in the first comment of this issue?

from ruby-mqtt.

JakubSzajna avatar JakubSzajna commented on June 11, 2024

@phlegx Yes, it does have this definition and it branches out from stdlib Exception.

rescue Exception => exp in MQTT module refers to MQTT::Exception instead of ::Exception though.
So it would catch exceptions like MQTT::Exception and everything that inherits from MQTT::Exception.
Errno::ECONNRESET does not have MQTT::Exception in it's ancestors chain, so it won't be catched in the rescue clause you mentioned.

The monkey-patch from my message wraps the whole receive_packet in the rescue clause, so every exception that will be raised inside receive_packet will be catched there and elevated to the parent thread. Hope this is the answer for your question :) It is supposed to make sure the exception from your initial message (and others from inside the receive_packet method) can be rescued. So this does not actually rescue from the exception, it just makes it rescue'able :) To catch this exception, use a rescue clause in a scope where you use MQTT::Client.connect method.

from ruby-mqtt.

phlegx avatar phlegx commented on June 11, 2024

@JakubSzajna can you write a PR please?

from ruby-mqtt.

phlegx avatar phlegx commented on June 11, 2024

Found similar PR #112 related to StandardError.

from ruby-mqtt.

Toucouleur66 avatar Toucouleur66 commented on June 11, 2024

I don't think this fixes the issue were talking about here. If I find some time, I'll create a PR to include a proposed solution in the gems code.

any hope for this PR?

from ruby-mqtt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.