Skip to content

fix(bt/osi): stop timer before re-arming in alarm_set (IDFGH-17864)#18762

Open
anuragak021 wants to merge 1 commit into
espressif:masterfrom
anuragak021:fix/bt-osi-alarm-set-double-start
Open

fix(bt/osi): stop timer before re-arming in alarm_set (IDFGH-17864)#18762
anuragak021 wants to merge 1 commit into
espressif:masterfrom
anuragak021:fix/bt-osi-alarm-set-double-start

Conversation

@anuragak021

@anuragak021 anuragak021 commented Jun 26, 2026

Copy link
Copy Markdown

Summary

alarm_set() calls esp_timer_start_once / esp_timer_start_periodic without first stopping the timer handle. If the Bluedroid stack re arms an alarm that is already running (e.g. a scheduling alarm fired before the previous instance was cancelled), esp_timer_start_* returns ESP_ERR_INVALID_STATE and the alarm is silently dropped, producing the log error:

E (xxxx) BT_OSI: alarm_set failed to start timer, err 0x103

alarm_free() already uses the correct pattern it calls esp_timer_stop before esp_timer_delete. This fix applies the same stop before start in alarm_set so that a double-set reschedules the alarm rather than failing.

Change

Add esp_timer_stop(alarm->alarm_hdl) before the esp_timer_start_once / esp_timer_start_periodic call inside the static alarm_set() helper in components/bt/common/osi/alarm.c.

esp_timer_stop returns ESP_ERR_INVALID_STATE when the timer is already stopped, which is expected and harmless; the subsequent start call then proceeds normally.

Reproduction

Observed on ESP32 S3 with CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST=y. PSRAM allocation latency shifts task scheduling enough that osi_alarm_set is called on a running timer handle during BLE GAP advertising setup, triggering the error on every boot.

Test

Build with Bluedroid enabled (CONFIG_BT_BLUEDROID_ENABLED=y, CONFIG_BT_BLE_ENABLED=y) and start BLE advertising the alarm_set failed error no longer appears.


Note

Low Risk
Single-line behavioral fix in BT timer scheduling aligned with existing alarm_free() usage; no auth, data, or API surface changes.

Overview
Fixes Bluedroid alarms being silently dropped when osi_alarm_set / osi_alarm_set_periodic runs on a handle that is still active. esp_timer_start_once and esp_timer_start_periodic return ESP_ERR_INVALID_STATE in that case, which surfaced as alarm_set failed to start timer, err 0x103 during BLE GAP advertising on ESP32-S3 with SPIRAM-first BT allocation.

alarm_set() in alarm.c now calls esp_timer_stop on the handle before starting the timer, matching the stop-then-operate pattern already used in alarm_free(). Re-arming reschedules the alarm instead of failing; stopping an already-idle timer is harmless (ESP_ERR_INVALID_STATE is ignored).

Reviewed by Cursor Bugbot for commit b7af0d4. Bugbot is set up for automated code reviews on this repo. Configure here.

alarm_set() called esp_timer_start_once/periodic without first stopping
the timer handle. If the same alarm is set while it is already running
(re-armed before it fires or is cancelled), esp_timer_start_* returns
ESP_ERR_INVALID_STATE and the alarm is silently dropped, logged as:

  E BT_OSI: alarm_set failed to start timer, err 0x103

alarm_free() already calls esp_timer_stop before esp_timer_delete as
the correct pattern. Apply the same stop-before-start here so that a
double-set reschedules the alarm rather than failing silently.
@CLAassistant

CLAassistant commented Jun 26, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@espressif-bot espressif-bot added the Status: Opened Issue is new label Jun 26, 2026
@github-actions github-actions Bot changed the title fix(bt/osi): stop timer before re-arming in alarm_set fix(bt/osi): stop timer before re-arming in alarm_set (IDFGH-17864) Jun 26, 2026
@anuragak021

Copy link
Copy Markdown
Author

Reproduced consistently on ESP32-S3 custom board with CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST=y, IDF v5.5.2. Happy to test any backport builds against release/v5.5 if that helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Status: Opened Issue is new

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants