Skip to content

Fix file descriptor leaks causing server instability#7

Open
amalg wants to merge 1 commit intoohAnd:mainfrom
amalg:fix/file-descriptor-leaks
Open

Fix file descriptor leaks causing server instability#7
amalg wants to merge 1 commit intoohAnd:mainfrom
amalg:fix/file-descriptor-leaks

Conversation

@amalg
Copy link

@amalg amalg commented Feb 2, 2026

Summary

This PR addresses file descriptor exhaustion (OSError errno 24) that causes the TRMNL server to become unresponsive after extended operation.

Root Cause: PIL Image objects were not being explicitly closed, causing file handles to accumulate over time until the OS limit was reached.

Changes

  • Add requests.Session for HTTP connection pooling instead of creating new connections for each request
  • Fix add_footer_to_image() to explicitly close PIL Image objects using try/finally
  • Fix get_no_image() to properly close PIL Image object
  • Fix api_log() to handle different JSON structures from clients (some send {"log": {"logs_array": [...]}}, others send {"logs": [...]})
  • Add error handling for file descriptor exhaustion in QuietWSGIServer
  • Add error handling for file operations in persist functions
  • Close HTTP session on server shutdown in handle_exit()

Test Plan

  • Server runs without file descriptor exhaustion over extended periods
  • Image modification still works correctly
  • API endpoints respond properly
  • Different client JSON formats handled correctly

Evidence

Before fix - server logs showed:

OSError: [Errno 24] No file descriptors available
AttributeError: 'NoneType' object has no attribute 'get'

After fix - server remains stable without resource exhaustion.

This commit addresses file descriptor exhaustion (OSError errno 24) that
causes the TRMNL server to become unresponsive after extended operation.

Changes:
- Add requests.Session for HTTP connection pooling instead of creating
  new connections for each request
- Fix add_footer_to_image() to explicitly close PIL Image objects using
  try/finally to prevent file descriptor leaks
- Fix get_no_image() to properly close PIL Image object
- Fix api_log() to handle different JSON structures from clients
  (some send {"log": {"logs_array": [...]}}, others send {"logs": [...]})
- Add error handling for file descriptor exhaustion in QuietWSGIServer
- Add error handling for file operations in persist_log, persist_client_data,
  and persist_client_log_data functions
- Close HTTP session on server shutdown in handle_exit()

The root cause was PIL Image objects not being explicitly closed, which
caused file handles to accumulate over time until the OS limit was reached.
amalg added a commit to amalg/ha_addons that referenced this pull request Feb 2, 2026
Updated submodule to use fork with fix for file descriptor exhaustion
(OSError errno 24) that causes the server to become unresponsive.

See: ohAnd/trmnlServer#7
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.

1 participant