Add graceful I2C error handling to prevent printer shutdowns#38
Conversation
With Klipper PR #7013, I2C errors now trigger host-side shutdowns instead of MCU-side shutdowns. This change catches mcu.error exceptions during measurement cycles, allowing the sensor to log warnings and continue operating with previous values rather than shutting down the printer on transient I2C communication failures.
|
I'm gonna leave this on a branch while I look into Kalico's plans to pull in upstream changes. |
|
Thank you!! I'm gonna test this out tonight on my Stealthmax by simply unplugging one of the sensor stacks during operation to see what happens. So glad we might finally at long last have sane error handling for non-critical I2C sensors :) |
|
let me know how it goes. i haven't done any testing yet. |
|
Attempted testing, but klippy still crashes when I yank the wire going to my sensor stacks. attaching shortened klippy.log here for you, but the relevant commands when we crash are at line 924: Now this was an extreme example (I yanked the entire module out effectively, so not quite a read failure the way it was expected |
|
Commented out my BME280 to clean up the error messages (since you're not touching the BME280 code I think that still fails ungracefully): From what I can tell, it looks like the code after the I2C exception is unable to handle the i2c_response having no data (I think). But it's tough to say since Python's not my strongest language Log here: klippy_i2c_fail_sgp40_only.log |
|
Unfortunately, even with the changes I made above, we still crash out: |
- Catch generic Exception instead of mcu.error (which wasn't being raised) - Use logging.exception() to get full traceback - Remove unused mcu import Fixes the actual exception type being raised (gcode.CommandError) which was causing crashes during testing.
|
Okay @thetic, I tried the new code. Unfortunately still not working, but I got some insight from the Klipper dev who wrote PR #7013. It seems the problem for my test is here in bus.py: It seems like unless the MCU reports that the I2C command was a SUCCESS (not I2C_READ_NACK, I2C_NACK, etc), it automatically shuts down the printer. So by force-disconnecting the device I'm automatically killing things because I guarantee a NACK. Going to let my printer run overnight and see what happens - I haven't seen what happens with a simple read failure. |
|
Hey! I volunteer myself to test the new thing when it's test ready. Thank you! It's frustrating to me, I have a nevermore stealthmax pcb v2 which is basically a glorified fan controller now. Thanks to this project I'll be able to bring it back to life. |
|
@ndanyluk any news/progress? It sounded like you were following some supporting changes upstream, but I've been preoccupied with some personal issues these past few months. Thanks for all your help so far. |
|
Closing in favor of a new approach at #38 |
Summary
Add graceful I2C error handling to prevent printer shutdowns from transient I2C communication failures.
Context
Klipper PR #7013 (merged Feb 7, 2026) changed I2C error handling from MCU-side to host-side. I2C errors now raise
mcu.errorexceptions in Python that trigger printer shutdowns if not caught.This PR adds try-except blocks around I2C operations in the measurement loop to catch these exceptions, log warnings, and continue operating with previous sensor values rather than shutting down the printer.
Changes
Compatibility
Klipper (current): This change is effective immediately for users on Klipper v0.13.0+ with PR #7013's host-side error handling.
Kalico (current): Kalico does not yet have Klipper's PR #7013 changes. On Kalico, I2C errors still trigger MCU-side shutdowns that occur below the Python layer, so this error handling won't prevent those shutdowns. However, the changes are forward-compatible and will become effective when Kalico syncs these upstream changes.