Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/Playwright/Playwright.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,41 @@ public static async Task<IPlaywright> CreateAsync()
{
var transport = new StdIOTransport();
var connection = new Connection();
transport.MessageReceived += (_, message) =>

EventHandler<byte[]> onMessageReceived = (_, message) =>
{
Connection.TraceMessage("pw:channel:recv", message);
connection.Dispatch(JsonSerializer.Deserialize<PlaywrightServerMessage>(message, JsonExtensions.DefaultJsonSerializerOptions)!);
};
transport.LogReceived += (_, log) =>
EventHandler<string> onLogReceived = (_, log) =>
{
// workaround for https://github.com/nunit/nunit/issues/4144
var writer = Environment.GetEnvironmentVariable("PWAPI_TO_STDOUT") != null ? Console.Out : Console.Error;
writer.WriteLine(log);
};
transport.TransportClosed += (_, reason) => connection.DoClose(reason);
EventHandler<Exception> onTransportClosed = (_, reason) => connection.DoClose(reason);

transport.MessageReceived += onMessageReceived;
transport.LogReceived += onLogReceived;
transport.TransportClosed += onTransportClosed;
connection.OnMessage = (message, keepNulls) =>
{
var rawMessage = JsonSerializer.SerializeToUtf8Bytes(message, keepNulls ? connection.DefaultJsonSerializerOptionsKeepNulls : connection.DefaultJsonSerializerOptions);
Connection.TraceMessage("pw:channel:send", rawMessage);
return transport.SendAsync(rawMessage);
};
connection.Close += (_, reason) => transport.Close(reason);
connection.Close += (_, reason) =>
{
// Break Connection<->Transport closure cycles and release the underlying
// process / token / reader task, so that callers who call `Dispose` on a
// short-lived Playwright don't accumulate orphaned graphs.
transport.MessageReceived -= onMessageReceived;
transport.LogReceived -= onLogReceived;
transport.TransportClosed -= onTransportClosed;
connection.OnMessage = null!;
transport.Close(reason);
transport.Dispose();
};
return await connection.InitializePlaywrightAsync().ConfigureAwait(false);
}
}
28 changes: 19 additions & 9 deletions src/Playwright/Transport/StdIOTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,8 @@ internal StdIOTransport()
{
_process = GetProcess("run-driver");
StartProcessWithUTF8IOEncoding(_process);
_process.Exited += (_, _) => Close(new TargetClosedException("Process exited"));
_process.ErrorDataReceived += (_, error) =>
{
if (error.Data != null)
{
LogReceived?.Invoke(this, error.Data);
}
};
_process.Exited += OnProcessExited;
_process.ErrorDataReceived += OnProcessErrorDataReceived;
_process.BeginErrorReadLine();

_getResponseTask = ScheduleTransportTaskAsync(GetResponseAsync, _readerCancellationSource.Token);
Expand Down Expand Up @@ -195,11 +189,27 @@ private void Dispose(bool disposing)
return;
}

if (_process != null)
{
_process.Exited -= OnProcessExited;
_process.ErrorDataReceived -= OnProcessErrorDataReceived;
_process.Dispose();
}
_readerCancellationSource?.Dispose();
_process?.Dispose();
_getResponseTask?.Dispose();
}

private void OnProcessExited(object? sender, EventArgs e)
=> Close(new TargetClosedException("Process exited"));

private void OnProcessErrorDataReceived(object? sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
LogReceived?.Invoke(this, e.Data);
}
}

private async Task GetResponseAsync(CancellationToken token)
{
try
Expand Down
Loading