Printer freezing/pausing during a print with Resource Monitor

Hello,

I'm the author of the "Resource Monitor" plugin and have had reports of users having issues with my plugin.
Apparently, in some (rare ?) cases the printer can freeze during a print (for ~1s) when my plugin is enabled. (Issue #37).

While I could not reproduce the issue with my printer, I have found something strange in the logs.

2022-04-17 03:34:23,084 - octoprint.plugins.resource_monitor - DEBUG - disk_usage(partition["mountpoint"] : sdiskusage(total=264289280, used=50511360, free=213777920, percent=19.1)
2022-04-17 03:34:23,092 - octoprint.plugins.resource_monitor - DEBUG - net_io_counters(pernic=True) : {'lo': snetio(bytes_sent=8254966181, bytes_recv=8254966181, packets_sent=1566153, packets_recv=1566153, errin=0, errout=0, dropin=0, dropout=0), 'eth0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'wlan0': snetio(bytes_sent=0, bytes_recv=0, packets_sent=0, packets_recv=0, errin=0, errout=0, dropin=0, dropout=0), 'wlan1': snetio(bytes_sent=8661301154, bytes_recv=307487609, packets_sent=6672247, packets_recv=3084668, errin=0, errout=0, dropin=0, dropout=0)}
2022-04-17 03:34:23,096 - octoprint.plugins.resource_monitor - DEBUG - net_if_addrs() : {'lo': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='127.0.0.1', netmask='255.0.0.0', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 10>, address='::1', netmask='ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='00:00:00:00:00:00', netmask=None, broadcast=None, ptp=None)], 'wlan1': [snicaddr(family=<AddressFamily.AF_INET: 2>, address='192.168.50.250', netmask='255.255.255.0', broadcast='192.168.50.255', ptp=None), snicaddr(family=<AddressFamily.AF_INET6: 10>, address='fe80::a962:ec0c:2a2b:3641%wlan1', netmask='ffff:ffff:ffff:ffff::', broadcast=None, ptp=None), snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='ac:22:0b:96:be:48', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)], 'eth0': [snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='b8:27:eb:84:99:af', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)], 'wlan0': [snicaddr(family=<AddressFamily.AF_PACKET: 17>, address='b8:27:eb:d1:cc:fa', netmask=None, broadcast='ff:ff:ff:ff:ff:ff', ptp=None)]}
2022-04-17 03:34:23,954 - octoprint.plugins.resource_monitor - DEBUG - net_if_stats() : {'lo': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=65536), 'eth0': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=65535, mtu=1500), 'wlan0': snicstats(isup=False, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500), 'wlan1': snicstats(isup=True, duplex=<NicDuplex.NIC_DUPLEX_UNKNOWN: 0>, speed=0, mtu=1500)}
2022-04-17 03:34:23,956 - octoprint.plugins.resource_monitor - DEBUG - sensors_battery() : None
2022-04-17 03:34:24,003 - octoprint.plugins.resource_monitor - DEBUG - cpu_freq() : scpufreq(current=700.0, min=600.0, max=1200.0)
2022-04-17 03:34:24,008 - octoprint.plugins.resource_monitor - DEBUG - cpu_percent(percpu=True) : [7.2, 0.0, 3.1, 8.0]

I'm using psutil to get all the information that is displayed in Resource Monitor. It looks like the call to psutil.net_if_stats() can take nearly one second to complete (in this case nearly 900ms) and I really don't know what could cause this.

Could this be the cause of the freezes ?
Maybe this is linked to this psutil issue.

I would really appreciate if someone could give a second look and help me with this.

If this ends up being an issue with psutil, I'll try to open an issue there.

Thanks for any help

I would assume your polling of the stats would be running in it's own thread utilizing octoprint.util.RepeatedTimer or similar. If that is the case not sure how that interaction could interfere with printing unless it's causing some kind of serial/USB contention while gathering stats.

Hello,

Yes, that is done using a RepeatedTimer. That's what got me a bit confused.
I quickly looked at psutil source code and didn't spot any obvious interaction with serial ports.

I'm not too sure how the GIL works with C function calls, but my only theory is that there might be a C call that hangs in psutil and that might prevent other threads from running. If not, I really don't know what could cause this.