diff --git a/lib/cosmos/top_level.rb b/lib/cosmos/top_level.rb index 4cf5b0b5a..7858708d2 100644 --- a/lib/cosmos/top_level.rb +++ b/lib/cosmos/top_level.rb @@ -244,6 +244,11 @@ def self.marshal_dump(marshal_filename, obj) rescue Exception # Oh well - we tried end + if exception.class == TypeError and exception.message =~ /Thread::Mutex/ + original_backtrace = exception.backtrace + exception = exception.exception("Mutex exists in a packet. Note: Packets must not be read during class initializers for Conversions, Limits Responses, etc.: #{exception}") + exception.set_backtrace(original_backtrace) + end self.handle_fatal_exception(exception) end end diff --git a/spec/top_level/top_level_spec.rb b/spec/top_level/top_level_spec.rb index a04ca3a15..cad16107c 100644 --- a/spec/top_level/top_level_spec.rb +++ b/spec/top_level/top_level_spec.rb @@ -171,6 +171,19 @@ def self.cleanup_exceptions Cosmos.cleanup_exceptions() end + it "rescues marshal dump errors in a Packet with a Mutex" do + capture_io do |stdout| + system_exit_count = $system_exit_count + pkt = Packet.new("TGT","PKT") + pkt.append_item("ITEM", 16, :UINT) + pkt.read_all + Cosmos.marshal_dump('marshal_test', pkt) + expect($system_exit_count).to be > system_exit_count + expect(stdout.string).to match("Mutex exists in a packet") + end + Cosmos.cleanup_exceptions() + end + it "rescues marshal load errors" do # Attempt to load something that doesn't exist expect(Cosmos.marshal_load('blah')).to be_nil