Skip to content

Conversation

@rm5248
Copy link

@rm5248 rm5248 commented Dec 22, 2025

Use a context manager when locking in python to ensure that if an exception is thrown, the lock will be released.

Add some basic error checking to the submit_event function to make sure that card read events have the correct data set.

fixes: #267

Copy link

Copilot AI left a comment

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 improves error handling and thread safety in the Python OSDP peripheral device implementation. The changes use context managers for lock management to ensure locks are released even when exceptions occur, and add validation for card read events to catch malformed event data early.

Key changes:

  • Replaced manual lock.acquire()/lock.release() calls with context manager pattern (with lock:) for exception safety
  • Added input validation to submit_event() to verify event structure and required fields for card read events
  • Imported Event and CardFormat constants to support the new validation logic

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@sidcha
Copy link
Member

sidcha commented Dec 22, 2025

@rm5248 Please ignore the AI review, I'm just trying it out.

raise TypeError("event does not contain 'event' key")

if event['event'] == Event.CardRead:
expected_keys = ["reader_no", "format", "direction", "data"]
Copy link
Member

Choose a reason for hiding this comment

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

These are checked in osdp_sys and the missing keys are reported from there (see here). If this info is insufficient, the code there needs to be extended to avoid code duplication.

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.

Python example only works with ASCII event

2 participants