elixir-circuits / circuits_uart

Discover and use UARTs and serial ports in Elixir
Apache License 2.0
188 stars 48 forks source link

Should we monitor/link to the `controlling_process`? #125

Open ewildgoose opened 2 years ago

ewildgoose commented 2 years ago

At present the library links when started with start_link (which is expected). However, consider that we start the library under a supervisor, and then some other genserver calls controlling_process and open, etc. If this process exits it seems desirable (to me) to close the serial port?

Now I can trap exits in the genserver process, but that leads to a rabbit hole of how tricky that is to get right, and the main alternative would be for the genserver to link to the circuits_uart process. However, I can't quite decide if that's a good idea or not? It has implications good and bad about what happens if the genserver process crashes during a message transmission over the serial port? However, the counter point is that closing the serial port also has some similar implications (although I guess we can choose to guard these by draining sending buffers on termination, etc?)

I'm genuinely not sure what is the right answer, but I notice that we are modelling this after gen_tcp and the like, and those will monitor (and link) to the process passed in controlling_process. Should the same logic be used here? (Possibly then with some concerns about whether to drain transmit buffers on close?)

fhunleth commented 2 years ago

Hi @ewildgoose - thanks for posting about this!

it's been a while since I've actively maintained circuits_uart. I think that if I were to revisit that I'd change a couple things and the supervision structure definitely sounds like it needs more thought. I think that you're right about closing the serial port and at least doing a best effort attempt at flushing transmit buffers.

If you want to start a PR, I'll try to allocate some time for the library.