Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 32987b5f authored by Simon Chan's avatar Simon Chan
Browse files

feat(adb): improve connection close handling

parent 704e70a5
Loading
Loading
Loading
Loading
+11 −7
Original line number Diff line number Diff line
@@ -169,6 +169,14 @@ function _Connect(): JSX.Element | null {
            return;
        }

        function dispose() {
            // Adb won't close the streams,
            // so manually close them.
            try { readable.cancel(); } catch { }
            try { writable.close(); } catch { }
            globalState.setDevice(undefined, undefined);
        }

        try {
            const device = await Adb.authenticate(
                { readable, writable },
@@ -177,20 +185,16 @@ function _Connect(): JSX.Element | null {
            );

            device.disconnected.then(() => {
                globalState.setDevice(undefined, undefined);
                dispose();
            }, (e) => {
                globalState.showErrorDialog(e);
                globalState.setDevice(undefined, undefined);
                dispose();
            });

            globalState.setDevice(selectedBackend, device);
        } catch (e: any) {
            globalState.showErrorDialog(e);

            // The streams are still open when Adb authentication failed,
            // manually close them to release the device.
            readable.cancel();
            writable.close();
            dispose();
        } finally {
            setConnecting(false);
        }
+3 −3
Original line number Diff line number Diff line
import { AdbPacketHeader, AdbPacketSerializeStream, DuplexStreamFactory, pipeFrom, ReadableStream, type AdbBackend, type AdbPacketData, type AdbPacketInit, type ReadableWritablePair, type WritableStream } from '@yume-chan/adb';
import { AdbPacketHeader, AdbPacketSerializeStream, DuplexStreamFactory, pipeFrom, ReadableStream, WritableStream, type AdbBackend, type AdbPacketData, type AdbPacketInit, type ReadableWritablePair } from '@yume-chan/adb';
import type { StructDeserializeStream } from "@yume-chan/struct";

export const ADB_DEVICE_FILTER: USBDeviceFilter = {
@@ -77,14 +77,14 @@ export class AdbWebUsbBackendStream implements ReadableWritablePair<AdbPacketDat
        }));

        this._writable = pipeFrom(
            factory.createWritable({
            factory.createWritable(new WritableStream({
                write: async (chunk) => {
                    await device.transferOut(outEndpoint.endpointNumber, chunk);
                },
            }, {
                highWaterMark: 16 * 1024,
                size(chunk) { return chunk.byteLength; },
            }),
            })),
            new AdbPacketSerializeStream()
        );
    }
+3 −3
Original line number Diff line number Diff line
import { AdbPacket, AdbPacketSerializeStream, DuplexStreamFactory, pipeFrom, ReadableStream, StructDeserializeStream, type AdbBackend } from '@yume-chan/adb';
import { AdbPacket, AdbPacketSerializeStream, DuplexStreamFactory, pipeFrom, ReadableStream, StructDeserializeStream, WritableStream, type AdbBackend } from '@yume-chan/adb';

export default class AdbWsBackend implements AdbBackend {
    public readonly serial: string;
@@ -42,14 +42,14 @@ export default class AdbWsBackend implements AdbBackend {
            size(chunk) { return chunk.byteLength; },
        }));

        const writable = factory.createWritable({
        const writable = factory.createWritable(new WritableStream({
            write: (chunk) => {
                socket.send(chunk);
            },
        }, {
            highWaterMark: 16 * 1024,
            size(chunk) { return chunk.byteLength; },
        });
        }));

        return {
            readable: readable.pipeThrough(new StructDeserializeStream(AdbPacket)),
+12 −15
Original line number Diff line number Diff line
@@ -76,6 +76,7 @@ export class Adb implements Closeable {
            await writer.write(calculateChecksum(init));
        }

        let banner: string;
        try {
            // https://android.googlesource.com/platform/packages/modules/adb/+/79010dc6d5ca7490c493df800d4421730f5466ca/transport.cpp#1252
            // There are some other feature constants, but some of them are only used by ADB server, not devices (daemons).
@@ -109,15 +110,16 @@ export class Adb implements Closeable {
                payload: encodeUtf8(`host::features=${features};`),
            });

            const banner = await resolver.promise;

            // Stop piping before creating `Adb` object
            // Because `AdbPacketDispatcher` will lock the streams when initializing
            banner = await resolver.promise;
        } finally {
            // When failed, release locks on `connection` so the caller can try again.
            // When success, also release locks so `AdbPacketDispatcher` can use them.
            abortController.abort();
            writer.releaseLock();

            // Wait until pipe stops (`ReadableStream` lock released)
            await pipe;
        }

        return new Adb(
            connection,
@@ -125,11 +127,6 @@ export class Adb implements Closeable {
            maxPayloadSize,
            banner,
        );
        } catch (e) {
            abortController.abort();
            writer.releaseLock();
            throw e;
        }
    }

    private readonly dispatcher: AdbPacketDispatcher;
+1 −1
Original line number Diff line number Diff line
@@ -175,7 +175,7 @@ export class AdbSync extends AutoDisposable {

    public override async dispose() {
        super.dispose();
        this.stream.close();
        await this.stream.close();
        await this.writer.close();
    }
}
Loading