Skip to content

refactor: isolatesocket and observatory - #948

Merged
KazuCocoa merged 1 commit into
mainfrom
refactor
Oct 30, 2026
Merged

KazuCocoa merged 1 commit into
mainfrom
refactor

Conversation

@KazuCocoa

Copy link
Copy Markdown
Member

No description provided.

@KazuCocoa
KazuCocoa merged commit 136a65a into main Oct 30, 2026
11 of 14 checks passed
@KazuCocoa
KazuCocoa deleted the refactor branch October 30, 2026 04:53
theluckystrike added a commit to theluckystrike/appium-flutter-driver that referenced this pull request Nov 2, 2026
…nnectSocket

Review follow-up for the two gaps in the previous commit.

The socket.call wrapper in connectSocket logged and swallowed every
error, so a call rejected by the disconnect resolved with undefined.
executeElementCommand turned that into an error, but enterText,
requestData, setFrameSync and the other handlers that call
executeSocketCommand directly returned undefined as a success. The
wrapper now rethrows DartVmConnectionClosedError, the error used to
reject pending calls on `close`. A call made while the connection is
down ("socket not ready") is rethrown as the same error. Other errors
are still logged and swallowed as before.

During the initial VM lookup the rejected getVM call made the async
`open` listener read `vm.isolates` of undefined. Nothing awaited the
listener, so the TypeError was an unhandled rejection and the
connection promise never settled. The setup now runs in setUpSocket,
and the `open` listener settles the connection promise with a failure
(and closes the socket) when setUpSocket rejects. connectSocket then
throws its usual "Cannot connect to the Dart Observatory URL" error.
Since appium#948 the isolate lookup runs inside retryInterval. A closed
connection can't recover, so the lookup stops retrying on
DartVmConnectionClosedError and fails the setup right away instead of
retrying for 15 seconds.

Tests: a disconnect during getVM rejects connectSocket without an
unhandled rejection, and flutter:enterText through `execute` rejects
on a disconnect, both during the call and right after it.
KazuCocoa pushed a commit that referenced this pull request Nov 3, 2026
…tion closes (#937)

* fix(driver): fail commands instead of hanging when the Dart VM connection closes

rpc-websockets 10.0.1 keeps a pending call until a reply or its timeout
arrives, and executeSocketCommand sets no timeout. When the Dart VM
socket closes during a command (app crash or relaunch), the call never
settles and the Appium command hangs until newCommandTimeout.

IsolateSocket now rejects pending calls on `close`. rpc-websockets
emits `close` only after it set `ready` to false, so calls made after
the drop are already refused with "socket not ready" and only calls from
the dead socket are in the queue. The connect wrapper turns a rejection
into an undefined result. executeElementCommand now throws a clear error
for an empty reply instead of a TypeError on `data.isError`. The wrapper
also logged `JSON.stringify(e)`, which is `{}` for an Error. It now logs
the Error itself, so the server log names the cause the new message
points to.

Fixes #733

I ran these in driver/ on this commit (main base e13a971), Node 22.23.2.

```
$ npm test
main lib + new tests  tests 7  pass 5  fail 2
  rejects a pending command when the Dart VM connection closes            test timed out after 5000ms
  fails an element command instead of hanging when the Dart VM ...        test timed out after 5000ms
this commit           tests 7  pass 7  fail 0
$ npm run lint
exit 0
$ npm run format:check
All matched files use the correct format.
```

End to end I ran the call the Appium router makes,
`FlutterDriver.executeCommand('execute', 'flutter:waitForAbsent', [finder])`,
against a fake Dart VM service. It drops the socket mid-command and
never replies, the way a crashing app does.

```
main
info Connection to ws://127.0.0.1:53066/ws closed
executeCommand('execute', 'flutter:waitForAbsent') -> STILL PENDING after 15000 ms (hang)
this commit
info Connection to ws://127.0.0.1:53104/ws closed
ERR! Error: The Dart VM connection was closed before it replied
executeCommand('execute', 'flutter:waitForAbsent') -> rejected: Cannot execute command
  waitForAbsent, no response from the Dart VM. Check the server log for the connection error [4 ms]
```

I didn't run the device functional workflow (driver-function.yml) because it
needs an iOS simulator with a built Flutter app.

* fix(driver): propagate Dart VM disconnects to every command and to connectSocket

Review follow-up for the two gaps in the previous commit.

The socket.call wrapper in connectSocket logged and swallowed every
error, so a call rejected by the disconnect resolved with undefined.
executeElementCommand turned that into an error, but enterText,
requestData, setFrameSync and the other handlers that call
executeSocketCommand directly returned undefined as a success. The
wrapper now rethrows DartVmConnectionClosedError, the error used to
reject pending calls on `close`. A call made while the connection is
down ("socket not ready") is rethrown as the same error. Other errors
are still logged and swallowed as before.

During the initial VM lookup the rejected getVM call made the async
`open` listener read `vm.isolates` of undefined. Nothing awaited the
listener, so the TypeError was an unhandled rejection and the
connection promise never settled. The setup now runs in setUpSocket,
and the `open` listener settles the connection promise with a failure
(and closes the socket) when setUpSocket rejects. connectSocket then
throws its usual "Cannot connect to the Dart Observatory URL" error.
Since #948 the isolate lookup runs inside retryInterval. A closed
connection can't recover, so the lookup stops retrying on
DartVmConnectionClosedError and fails the setup right away instead of
retrying for 15 seconds.

Tests: a disconnect during getVM rejects connectSocket without an
unhandled rejection, and flutter:enterText through `execute` rejects
on a disconnect, both during the call and right after it.

* fix(driver): stop the reconnect of a socket abandoned during setup

When the connection drops during getVM, rpc-websockets 10.0.1 clears
its socket and schedules a reconnect (reconnect_timer_id) before the
`close` event fires. Client.close() only closes an existing socket, so
the setup-failure path did nothing: the abandoned client reconnected
about a second later and left an unmanaged connection open. Turning
`reconnect` off alone isn't enough because the scheduled callback calls
_connect() anyway.

IsolateSocket.dispose() turns automatic reconnection off, clears the
scheduled reconnect timer and closes any open socket. connectSocket
calls it when the setup fails.

The regression test no longer closes the leaked reconnect from the
server. It now asserts that the server sees no new connection for 2 s
after the setup fails, which is longer than the 1 s reconnect interval.
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