Skip to content

Changes to affect disconnect stability #947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

adamb3ll
Copy link

@adamb3ll adamb3ll commented Aug 3, 2025

Summary

Enhanced the WebSockets library with ESP32-specific optimisations to prevent pbuf_free crashes and improve connection stability.

Changes Made

1. Enhanced dropNativeClient() Function

  • Added buffer cleanup timing for ESP32
  • Implemented 10ms delay before tcp->stop() to allow pending network operations to complete
  • Added 5ms delay after stop for additional cleanup time
  • Added yield() calls to prevent blocking during cleanup

2. Improved clientDisconnect() Function

  • Added safety checks before calling dropNativeClient()
  • Ensured network operations complete before cleanup
  • Added yield() to prevent race conditions

3. Enhanced clientIsConnected() Function

  • Added 5ms delays before disconnection calls to prevent rapid disconnection events
  • Improved timing to prevent overwhelming the network stack
  • Added yield() calls for better task management

4. Conservative Main Loop Functions

  • Added 5ms timing control to WebSocketsServerCore::loop() and WebSocketsServer::loop()
  • Prevents overwhelming the ESP32 network stack
  • Reduces frequency of WebSocket event processing

Technical Details

  • Target Platform: ESP32-specific optimizations using #if(WEBSOCKETS_NETWORK_TYPE == NETWORK_ESP32) conditionals
  • Root Cause: ESP32's lwIP stack requires more careful buffer management than other platforms
  • Solution: Conservative timing and proper cleanup sequences

Benefits

  • Eliminates pbuf_free crashes during WebSocket disconnections
  • Improves connection stability for long-running applications
  • Prevents network stack overload on ESP32
  • Maintains compatibility with other platforms (conditionals ensure changes only apply to ESP32)

Testing

  • Successfully tested with extended soak testing (>2 hour)
  • No crashes observed during rapid connection/disconnection cycles
  • Improved stability for permanent WebSocket connections

This enhancement specifically addresses ESP32's unique network stack requirements while maintaining full compatibility with other Arduino platforms.

@adamb3ll adamb3ll closed this Aug 4, 2025
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