Skip to content

Fix crash when deleting device while WebSocket is still connecting #20

Description

@jelledelaender

Summary

SoundTouch Bridge can crash when a device is deleted while its SoundTouch WebSocket is still in the connecting state. The crash happens because closeWebSocket() calls WebSocket.close() before the ws connection has been established.

Crash report

  • App ID: com.codingmammoth.soundtouchbridge
  • App version: v0.4.2
  • Homey version: v13.2.0
  • Homey model ID: shs
  • Homey model name: Homey Self-Hosted Server
  • Crash source: Homey crash report email
Error: WebSocket was closed before the connection was established
    at WebSocket.close (/app/node_modules/ws/lib/websocket.js:300:7)
    at SoundTouchDevice.closeWebSocket (/app/drivers/soundtouch/device.js:608:10)
    at SoundTouchDevice.onDeleted (/app/drivers/soundtouch/device.js:111:10)
    at SoundTouchDevice._onDeleted (/app/packages/homey-local/lib/AppProcess/node_modules/@athombv/homey-apps-sdk-v3/lib/Device.js:205:16)
    at process.processTicksAndRejections (node:internal/process/task_queues:103:5)
    at async SoundTouchDriver._onDeviceDelete (/app/packages/homey-local/lib/AppProcess/node_modules/@athombv/homey-apps-sdk-v3/lib/Driver.js:97:5)

Homey crash details URL:
https://tools.developer.homey.app/apps/app/com.codingmammoth.soundtouchbridge/build/8/crashes

Expected behavior

Deleting a device should clean up the WebSocket connection without throwing, regardless of whether the socket is open, connecting, closing, or already closed.

Likely area

  • drivers/soundtouch/device.js
  • SoundTouchDevice.closeWebSocket()
  • SoundTouchDevice.onDeleted()

Suggested fix direction

  • Guard WebSocket cleanup based on readyState.
  • For CONNECTING, terminate/detach handlers or wait for open/error before cleanup instead of calling close() in a way that throws.
  • Make closeWebSocket() idempotent and safe during device deletion.
  • Ensure timers/reconnect handlers cannot revive a deleted device connection after cleanup.

Acceptance criteria

  • Deleting a device while the WebSocket is connecting no longer crashes the app.
  • closeWebSocket() is safe for all relevant ws.readyState values.
  • WebSocket reconnect timers/listeners are cleaned up during deletion.
  • Add or update focused coverage where practical, or document manual verification if the Homey lifecycle is hard to unit test.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions