Skip to content

Fix LogConfig ID lifecycle - #608

Open
ataffanel wants to merge 7 commits into
masterfrom
ataffanel/fix-log-id-lifecycle
Open

Fix LogConfig ID lifecycle#608
ataffanel wants to merge 7 commits into
masterfrom
ataffanel/fix-log-id-lifecycle

Conversation

@ataffanel

@ataffanel ataffanel commented Sep 1, 2026

Copy link
Copy Markdown
Member

Abstract

Fix repeated LogConfig creation and deletion so applications can use logging for more than one byte-sized ID cycle without stale host-side blocks capturing reused IDs. Log block IDs are now managed as acknowledgement-owned leases, keeping host and firmware lifecycle state aligned across deletion, reset, disconnect, and concurrent commands.

Closes #577.

Why

The previous implementation incremented an ID counter modulo 255 but never removed acknowledged-deleted configurations from Log.log_blocks. Once an ID wrapped, reply and log-data lookup found the stale configuration first. This made the firmware appear to run out of log blocks after repeated create/read/delete operations even though it had deleted them correctly.

The fix waits for the firmware deletion acknowledgement before releasing an ID. This is important because releasing earlier could route an in-flight reply or data packet to a newer configuration using the same opaque handle.

What changed

  • Manage the full firmware-supported 0..255 ID range with a thread-safe FIFO free-ID pool.
  • Release and detach a configuration only after DELETE succeeds or returns ENOENT; failed deletion retains the lease and can be retried.
  • Make delete idempotent while its acknowledgement is pending.
  • Drain IDs on disconnect and reset, restoring the complete pool only after reset is acknowledged.
  • Serialize log lifecycle commands and validate registration after packet sends so reset, delete, disconnect, and synchronous driver callbacks cannot create stale follow-up commands.
  • Allow a detached LogConfig specification to be registered again with a fresh lease.
  • Resolve variables without an explicit fetch type fresh on each registration, without accumulating duplicates.
  • Raise LogConfigError for invalid lifecycle transitions while keeping detached cleanup calls harmless.

Verification

  • uv run python -m unittest discover ./test — 219 tests passed.
  • Configured pre-commit hooks passed for both changed files and on every commit.
  • Crazyflie over radio running the latest firmware:
    • 300 iterations using a newly constructed config each time passed.
    • 300 iterations re-registering one untyped config passed.
  • Final independent standards and specification reviews reported clean.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes the host-side LogConfig ID lifecycle so log block IDs can wrap and be reused safely without stale Log.log_blocks entries capturing replies/log data for reused IDs. It introduces an acknowledgement-owned “lease” model for log block IDs, aligning host and firmware state across delete/reset/disconnect and concurrent lifecycle commands.

Changes:

  • Replace modulo ID counter behavior with a thread-safe free-ID pool covering the full 0..255 range, releasing IDs only after delete acknowledgements.
  • Serialize log lifecycle commands (create/start/stop/delete/reset/disconnect) and validate registrations after packet sends to prevent stale follow-up actions.
  • Add comprehensive unit tests for ID reuse, delete/reset/disconnect behavior, retries, and concurrency.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
cflib/crazyflie/log.py Implements ID leasing, deferred release on delete ack, reset/disconnect draining, and command serialization with registration validation.
test/crazyflie/test_log.py Adds tests covering ID wrap/reuse, idempotent delete, reset/disconnect semantics, retry behavior, and concurrent registrations.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread cflib/crazyflie/log.py
Comment on lines +601 to +617
with self._registration_lock:
if self._reset_pending:
return
self._reset_pending = True
self._ids_ready = False
self._available_config_ids.clear()

pk = CRTPPacket()
pk.set_header(CRTPPort.LOGGING, CHAN_SETTINGS)
pk.data = (CMD_RESET_LOGGING,)
try:
self.cf.send_packet(
pk, expected_reply=(CMD_RESET_LOGGING,))
except Exception:
with self._registration_lock:
self._reset_pending = False
raise
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.

How to properly delete a LogConfig?

2 participants