---
title: "Task watchdog"
canonical_url: https://developer.repebble.com/pebbleos/docs/samples/subsys/task_wdt/stall/
last_updated: 2026-11-18
sdk_version: 4.44
---
# Task watchdog

Source: https://developer.repebble.com/pebbleos/docs/samples/subsys/task_wdt/stall/

This sample watches its main thread with the [task watchdog](https://developer.repebble.com/pebbleos/docs/subsystems/task_watchdog.md), on a channel that expires after a second without a feed.

Work that feeds the channel in time is never reported, and neither is waiting for work once the thread marks itself as waiting. Then the thread gets stuck on a semaphore nobody gives: the watchdog logs the stuck thread and calls the channel callback, which frees it. The sample overrides the `pbl_task_wdt_stalled()` and `pbl_task_wdt_recovered()` hooks to print what happens; the system uses them for its own reports.

On boards with a hardware watchdog, a channel that stayed expired for the grace period would reset the system. Here the callback recovers the thread first.

Run:

`pbl samples samples/subsys/task_wdt/stall`

Boards:

every board

## Sources

src/main.c

```
#include \<stdio.h\>#include \<pbl/kernel/delay.h\>#include \<pbl/kernel/sem.h\>#include \<pbl/task\_wdt/task\_wdt.h\>#define TIMEOUT\_MS 1000U#define STEP\_MS 200U#define STEPS 10U/\* What the main thread waits on when it gets stuck; only the watchdog callback gives it. \*/static PBL\_SEM\_DEFINE(s\_resource, 0, 1);static PBL\_SEM\_DEFINE(s\_recovered, 0, 1);static PBL\_SEM\_DEFINE(s\_work, 0, 1);/\* Runs on the watchdog thread on each check that finds the channel expired. \*/static void \*prv\_expired(int channel\_id, void \*user\_data) {printf("task\_wdt: channel %d expired, freeing what main waits on\n", channel\_id);pbl\_sem\_give(&s\_resource);return nullptr;}/\* Hooks the system can override: here they print, the defaults do nothing. \*/void pbl\_task\_wdt\_stalled(const struct pbl\_task\_wdt\_stall \*stall) {printf("task\_wdt: stalled, channels in use 0x%02x, fed in time 0x%02x\n", stall-\>active\_mask,stall-\>fed\_mask);}void pbl\_task\_wdt\_recovered(void) {printf("task\_wdt: recovered\n");pbl\_sem\_give(&s\_recovered);}int main(void) {int channel;channel = pbl\_task\_wdt\_add(nullptr, TIMEOUT\_MS, prv\_expired, nullptr);if (channel \< 0) {fprintf(stderr, "task\_wdt: no channel (%d)\n", channel);return channel;}printf("task\_wdt: main watched on channel %d, fed every %u ms at least\n", channel, TIMEOUT\_MS);/\* Busy work that feeds in time is never reported. \*/for (unsigned int i = 0; i \< STEPS; i++) {pbl\_sleep\_ms(STEP\_MS);pbl\_task\_wdt\_feed(channel);}printf("task\_wdt: %u ms of work fed in time\n", STEPS \* STEP\_MS);/\* Waiting for work is no stall: the channel does not expire meanwhile. \*/pbl\_task\_wdt\_set\_waiting(true);(void)pbl\_sem\_take(&s\_work, PBL\_MSEC(3 \* TIMEOUT\_MS));pbl\_task\_wdt\_set\_waiting(false);printf("task\_wdt: waited %u ms for work, not reported\n", 3 \* TIMEOUT\_MS);/\* Stuck on something that never comes, until the callback frees it. \*/printf("task\_wdt: main gets stuck\n");(void)pbl\_sem\_take(&s\_resource, PBL\_FOREVER);pbl\_task\_wdt\_feed(channel);printf("task\_wdt: main unblocked, fed again\n");(void)pbl\_sem\_take(&s\_recovered, PBL\_FOREVER);pbl\_task\_wdt\_delete(channel);printf("Sample done\n");return 0;}
```

## Output

```
task_wdt: main watched on channel \d+, fed every 1000 ms at least
task_wdt: 2000 ms of work fed in time
task_wdt: waited 3000 ms for work, not reported
task_wdt: main gets stuck
<KernelMain> not fed for \d+ ms
task_wdt: channel \d+ expired, freeing what main waits on
task_wdt: stalled, channels in use 0x[0-9a-f]{2}, fed in time 0x[0-9a-f]{2}
task_wdt: recovered
Sample done
```

Generated from PebbleOS [7753e801d](https://github.com/coredevices/pebbleos/commit/7753e801d93d20f2d37534ce2fa69d9952015237). This page is maintained in the pebbleos repository: [docs/samples/subsys/task\_wdt/stall.md](https://github.com/coredevices/pebbleos/blob/7753e801d93d20f2d37534ce2fa69d9952015237/docs/samples/subsys/task_wdt/stall.md).
