Skip to content

Add reentrancy protection for callback execution#216

Merged
nijel merged 5 commits intomasterfrom
copilot/fix-sms-callback-crash
Feb 6, 2026
Merged

Add reentrancy protection for callback execution#216
nijel merged 5 commits intomasterfrom
copilot/fix-sms-callback-crash

Conversation

Copy link
Contributor

Copilot AI commented Feb 6, 2026

Calling Gammu functions from within incoming event callbacks (SMS, Call, CB, USSD) corrupts the state machine and causes segfaults. This occurs because callbacks fire during phone operations when the library state is not reentrant-safe.

Implementation

  • Added in_callback flag to StateMachineObject to track callback execution state
  • Modified BEGIN_PHONE_COMM macro to check flag and raise RuntimeError on reentrancy attempt
  • Set/clear flag around all PyObject_Call invocations in CheckIncomingEvents
  • Added explicit check in Abort() which bypasses the macro

Behavior Change

Before:

def callback(sm, event_type, data):
    sm.GetSignalQuality()  # Segmentation fault

sm.SetIncomingCallback(callback)

After:

def callback(sm, event_type, data):
    sm.GetSignalQuality()  # RuntimeError: Can not call Gammu functions from within callback

sm.SetIncomingCallback(callback)

Testing

Added test_callback_reentrancy_protection to verify the guard triggers correctly and doesn't affect normal operation.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https /usr/lib/apt/methods/https (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Incoming SMS on a SIM800C USB modem will crash</issue_title>
<issue_description>When using SMS callbacks with a SIM800C modem, the call to ReadDevice crash the process:

gammu/src/gammu.c:430

Fatal Python error: Segmentation fault

Current thread 0x00007fd295440700 (most recent call first):
  File "/mnt/s/dd/Repos/ha/core/homeassistant/components/sms/gateway.py", line 44 in sms_pull
  File "/mnt/s/dd/Repos/ha/core/venv/lib/python3.8/site-packages/python_gammu-3.2.3-py3.8-linux-x86_64.egg/gammu/worker.py", line 247 in run
  File "/usr/lib/python3.8/threading.py", line 932 in _bootstrap_inner
  File "/usr/lib/python3.8/threading.py", line 890 in _bootstrap
(gdb) backtrace
gammu/python-gammu#0  0x00000000005f26bb in PyObject_Call ()
gammu/python-gammu#1  0x00007fffe12601fb in CheckIncomingEvents (sm=0x7fffe061c030) at gammu/src/gammu.c:430
gammu/python-gammu#2  0x00007fffe1262790 in StateMachine_ReadDevice (self=0x7fffe061c030, args=<optimized out>, kwds=<optimized out>)
    at gammu/src/gammu.c:893
gammu/python-gammu#3  0x0000000000504939 in ?? ()
gammu/python-gammu#4  0x000000000056acb6 in _PyEval_EvalFrameDefault ()
gammu/python-gammu#5  0x000000000050b6fe in ?? ()
gammu/python-gammu#6  0x000000000056fb87 in _PyEval_EvalFrameDefault ()
gammu/python-gammu#7  0x00000000005f5956 in _PyFunction_Vectorcall ()
gammu/python-gammu#8  0x000000000056acb6 in _PyEval_EvalFrameDefault ()
gammu/python-gammu#9  0x00000000005f5956 in _PyFunction_Vectorcall ()
gammu/python-gammu#10 0x000000000056acb6 in _PyEval_EvalFrameDefault ()
gammu/python-gammu#11 0x00000000005f5956 in _PyFunction_Vectorcall ()
gammu/python-gammu#12 0x000000000050b7f8 in ?? ()
gammu/python-gammu#13 0x00000000005f2702 in PyObject_Call ()
gammu/python-gammu#14 0x000000000065578c in ?? ()
gammu/python-gammu#15 0x0000000000675d48 in ?? ()
gammu/python-gammu#16 0x00007fffff586609 in start_thread (arg=<optimized out>) at pthread_create.c:477
gammu/python-gammu#17 0x00007fffff6c2293 in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:95
(gdb)

More info

(gdb) print *sm

$4 = {ob_base = {ob_refcnt = 5, ob_type = 0x7fffe127dca0 <StateMachineType>}, s = 0x7fffe0020010, DebugFile = 0x0,
  IncomingCallback = 0x0, SMSStatus = 0, MessageReference = 0, IncomingCallQueue = {0x0 <repeats 11 times>},
  IncomingSMSQueue = {0x0 <repeats 11 times>}, IncomingCBQueue = {0x0 <repeats 11 times>}, IncomingUSSDQueue = {
    0x0 <repeats 11 times>}, memory_entry_cache_type = 0, memory_entry_cache = 1, todo_entry_cache = 1,
  calendar_entry_cache = 1, mutex = 0x19534e0}

</issue_description>

<agent_instructions>Add a safeguard to block execution of Gammu functions from the callback. This is not safe and can corrupt the state machine.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@nijel Having stack trace from the segfault would help. Most likely this is actually an issue in the Gammu library. @nijel See https://wiki.archlinux.org/title/Debugging/Getting_traces#Getting_the_trace for guide to get trace from the core dump. @nijel Looking at https://github.com/home-assistant/core/pull/56552, the problem is probably in using Gammu functions from the callback, what is not safe. I'm not sure if this is somewhere documented, but it can definitely cause problems.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits February 6, 2026 09:46
Co-authored-by: nijel <212189+nijel@users.noreply.github.com>
Co-authored-by: nijel <212189+nijel@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix crash in SMS callbacks for SIM800C modem Add reentrancy protection for callback execution Feb 6, 2026
Copilot AI requested a review from nijel February 6, 2026 09:53
@nijel nijel marked this pull request as ready for review February 6, 2026 13:26
@nijel nijel enabled auto-merge (squash) February 6, 2026 13:27
@nijel nijel merged commit 2d79ba4 into master Feb 6, 2026
44 checks passed
@nijel nijel deleted the copilot/fix-sms-callback-crash branch February 6, 2026 13:33
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.

Incoming SMS on a SIM800C USB modem will crash

2 participants