Skip to content

adb.exe server inherits app handles when launched via AdbServer.Instance.StartServer #123

Open
@jimon

Description

@jimon

Describe the bug

When AdvancedSharpAdbClient is used as part of the bigger app, e.g. an app with listen sockets, starting adb server would make it inherit all open handles. Meaning if app doesn't close the handles on shutdown, they will outlive the app and not be available unless adb server is killed.

Practical example:

  • App opens tcp listen socket on port 4000
  • App starts adb server
  • App crashed due to some error
  • 4000 port is still bound because adb inherited it's handled

It's unfortunate side effect of trying to pass adb stdout/stderr back to the app.

When adb.exe start-server is invoked, it internally starts the daemon with handles inherited.

.NET itself also creates a process with all handles inherited unconditionally, see the long standing issue.

One possible way to solve this is to use UseShellExecute = true in launch code, this will lose stdout redirect though.

ProcessStartInfo psi = new(filename, command)
{
    CreateNoWindow = true,
    WindowStyle = ProcessWindowStyle.Hidden,
    UseShellExecute = false,
    RedirectStandardError = true,
    RedirectStandardOutput = true
};

Another way is to manually invoke winapi CreateProcess and pass stdout/stderr handles to it, making everything else as not inherited. This would be so much easier if .NET had support for it.

Yet another way is to explicitly mark handles as not inheritable, but this is not practical when integrating the framework into a bigger app.

Steps to reproduce the bug

  • open tcp listen socket on port X
  • starts adb server
  • exit the app without closing the socket

netstat -ano -p tcp still shows port X as active

Expected behavior

No response

Screenshots

No response

NuGet package version

None

.NET Platform

No response

Platform type

No response

System version

No response

IDE

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions