-
Notifications
You must be signed in to change notification settings - Fork 594
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Segfault in finalization with LibGpiodDriver #1849
Comments
Thanks for reporting this. If I understand correctly, the problem does not occur if the instances are not leaked? |
That is correct. I have not seen the crash without leaked instances. |
[Triage] @joperezr ping 😄 |
I probably won't have time to work on this this week. |
[Triage] we need to verify if this is an external issue and based on findings decide if we want to add any mitigations on our side while we wait on the fix. We also should verify if this is still an issue at all as they could've fixed it already. |
Based on my understanding of the libgpiod v1 documentation, I believe this is not a bug in libgpiod. This means that Please note that I no longer have the hardware for reproducing this issue, and I also have not looked at the |
@rtoijala Thanks for finding this reference, that was exactly what I was assuming but was unable to find a confirmation for. That likely means my proposed fix was actually right. I need to check that and update it once. |
Not sure where the "official" documentation is, but https://www.lane-fu.com/linuxmirror/libgpiod/doc/html/group______chips____.html seems pretty much to be the reference. |
Describe the bug
Leaking a GpioController that uses LibGpiodDriver and has a pin open can segfault due to the pin being released after the chip has been destroyed. This appears to be a race condition and does not occur always.
GpioController creates a SafeChipHandle for the chip and a SafeLineHandle for the pin/line. Destroying the chip invalidates the line, since libgpiod frees the line's memory when destroying the chip. I suspect that the reason for the crash is that during finalization, the runtime does not know that the chip should be destroyed last (or that the line should not be freed if the chip has already been freed).
Steps to reproduce
The code below reproduces the segfault within a few tries.
Expected behavior
The GPIO chip and line/pin are destroyed/released correctly. No segfault.
Actual behavior
Segfault.
See the attached log, generated with GDB. Unfortunately I do not have debug symbols for dotnet itself, but the stack trace for libgpiod is visible.
To summarize, what happens is:
gpiod_chip_get_line
)controller2
is destroyed, which also frees the line struct at0xaaaaaae0dbb0
.controller6
is destroyed. This is just noise.0xaaaaaae0dbb0
fromcontroller2
is released again, causing a crash because the memory has already been reused and overwritten.crash.txt
Versions used
dotnet --info
on the build machine (self-contained build for linux-arm64)System.Device.Gpio
version 2.1.0The text was updated successfully, but these errors were encountered: