Skip to content

Conversation

@zolakeith
Copy link

@zolakeith zolakeith commented Aug 20, 2024

When the SocketServer attempts to decode messages there is the possibility of error caused by bad data.

This commit adapts the code so that any exceptions caused by decoding are captured, and a warning is logged, and the process is stopped without causing an error.

The types of errors seen were:

  • bad return value: {:error, "" is not repeatable""}
  • (CondClauseError) no cond clause evaluated to a truthy value (coap_ex 0.1.0) lib/coap/message_options.ex
  • (CaseClauseError) no case clause matching <<...>> (coap_ex 0.1.0) lib/coap/message_options.ex:54
  • (FunctionClauseError) no function clause matching in CoAP.Block.decode/1
  • (MatchError) no match of right hand side value: """"

One could argue that these "errors" should be fixed by updating the MessageOptions module to gracefully handle such conditions, and that is definitely worth considering. By doing that, we would then be able to complete the decoding, which would allow extracting the message token which is required to identify the associated connection process.

Bad Return

GenServer #PID<0.12197.249> terminating** (stop) bad return value: {:error, "" is not repeatable""}

Last message: {:udp, #Port<0.1357880>, {10, 148, 4, 179}, 1200, <<227, 3, 7, 2, 0, 0, 0, 157, 87, 43, 90, 195, 102, 208, 55, 202, 12, 234, 12, 241, 12, 217, 12, 85, 250, 113, 61, 120, 0, 140, 192, 91, 13, 39, 0, 13, 0, 169, 4, 0, 0, 131, 92, 195, 102, ...>>}

This error happens because when a non repeatable option is encountered by the MessageOptions.Decoder, it throws an exception which is not caught, resulting in the SocketServer process returning the error from the handle_info/2 genserver callback. This causes the process to stop, because {:error, "" is not repeatable""} is not a valid handle_info/2 response.

Fixing this issue would involve "catching" the exception, and ensure the SocketServer is able to return a valid response from the handle_info/2 callback.

CondClauseError

GenServer #PID<0.3708.144> terminating** (CondClauseError) no cond clause evaluated to a truthy value 
(coap_ex 0.1.0) lib/coap/message_options.ex:73: CoAP.MessageOptions.Decoder.decode_extended/4 
(coap_ex 0.1.0) lib/coap/message_options.ex:51: CoAP.MessageOptions.Decoder.decode/3 
(coap_ex 0.1.0) lib/coap/message_options.ex:42: CoAP.MessageOptions.Decoder.options_and_payload/1 
(coap_ex 0.1.0) lib/coap/message.ex:241: CoAP.Message.decode/1 
(coap_ex 0.1.0) lib/coap/socket_server.ex:94: CoAP.SocketServer.handle_info/2 
(stdlib 3.17.2.1) gen_server.erl:695: :gen_server.try_dispatch/4 
(stdlib 3.17.2.1) gen_server.erl:771: :gen_server.handle_msg/6 
(stdlib 3.17.2.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3

Last message: {:udp, #Port<0.788077>, {10, 150, 51, 74}, 5683, <<100, 69, 0, 1, 239, 70, 20, 250, 68, 105, 29, 80, 244, 100, 69, 0, 1, 239, 70, 20, 250, 68, 105, 29, 80, 244>>}

This error happens because there is no clause which returns truthy - this cond could be updated to include a base case which throws an exception.

@zolakeith zolakeith force-pushed the ks/bad_return_value branch from 522b247 to f8c1396 Compare August 21, 2024 13:09
@zolakeith zolakeith changed the title Ks/bad return value Handling decoding errors Aug 21, 2024
@zolakeith zolakeith force-pushed the ks/bad_return_value branch from f8c1396 to 7581dca Compare August 21, 2024 13:41
@zolakeith zolakeith force-pushed the ks/bad_return_value branch from 7581dca to dbcd5ef Compare August 21, 2024 13:52
When the SocketServer attempts to decode messages there is the
possibility of error caused by bad data.

This commit adapts the code so that any exceptions caused by decoding
are captured, and a warning is logged, and the process is stopped
without causing an error.

The types of errors seen were:

- bad return value: {:error, "" is not repeatable""}
- (CondClauseError) no cond clause evaluated to a truthy value (coap_ex
  0.1.0) lib/coap/message_options.ex
- (CaseClauseError) no case clause matching <<...>> (coap_ex 0.1.0)
  lib/coap/message_options.ex:54
- (FunctionClauseError) no function clause matching in
  CoAP.Block.decode/1
- (MatchError) no match of right hand side value: """"

It might be better to update the MessageOptions module to handle these
errors gracefully by throwing a controlled exception. This exception
could then be caught by the SocketServer, allowing it to return a
relevant error along with the partially decoded message. This approach
would enable the SocketServer to potentially identify the connection and
send an appropriate error message to the client.
@zolakeith zolakeith force-pushed the ks/bad_return_value branch from dbcd5ef to 35cb537 Compare August 21, 2024 15:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants