Skip to content

Commit 05fb9e5

Browse files
authored
Merge pull request #1026 from softworkz/submit_disposed_exceptions
SocketIoFacade: Improve disposal, avoid exception on tear-down
2 parents 8009348 + 41c5b5a commit 05fb9e5

File tree

5 files changed

+51
-9
lines changed

5 files changed

+51
-9
lines changed

src/ElectronNET.API/API/App.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ public void Exit(int exitCode = 0)
427427

428428
public void DisposeSocket()
429429
{
430-
BridgeConnector.Socket.DisposeSocket();
430+
BridgeConnector.Socket.Dispose();
431431
}
432432

433433
/// <summary>

src/ElectronNET.API/Bridge/SocketIOFacade.cs

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ namespace ElectronNET.API;
88
using SocketIO.Serializer.SystemTextJson;
99
using SocketIO = SocketIOClient.SocketIO;
1010

11-
internal class SocketIoFacade
11+
internal class SocketIoFacade : IDisposable
1212
{
1313
private readonly SocketIO _socket;
1414
private readonly object _lockObj = new object();
15+
private bool _isDisposed;
1516

1617
public SocketIoFacade(string uri)
1718
{
@@ -27,6 +28,8 @@ public SocketIoFacade(string uri)
2728

2829
public void Connect()
2930
{
31+
this.CheckDisposed();
32+
3033
_socket.OnError += (sender, e) => { Console.WriteLine($"BridgeConnector Error: {sender} {e}"); };
3134

3235
_socket.OnConnected += (_, _) =>
@@ -46,6 +49,8 @@ public void Connect()
4649

4750
public void On(string eventName, Action action)
4851
{
52+
this.CheckDisposed();
53+
4954
lock (_lockObj)
5055
{
5156
_socket.On(eventName, _ => { Task.Run(action); });
@@ -54,6 +59,8 @@ public void On(string eventName, Action action)
5459

5560
public void On<T>(string eventName, Action<T> action)
5661
{
62+
this.CheckDisposed();
63+
5764
lock (_lockObj)
5865
{
5966
_socket.On(eventName, response =>
@@ -66,6 +73,8 @@ public void On<T>(string eventName, Action<T> action)
6673

6774
public void Once(string eventName, Action action)
6875
{
76+
this.CheckDisposed();
77+
6978
lock (_lockObj)
7079
{
7180
_socket.On(eventName, _ =>
@@ -78,6 +87,8 @@ public void Once(string eventName, Action action)
7887

7988
public void Once<T>(string eventName, Action<T> action)
8089
{
90+
this.CheckDisposed();
91+
8192
lock (_lockObj)
8293
{
8394
_socket.On(eventName, (socketIoResponse) =>
@@ -90,6 +101,11 @@ public void Once<T>(string eventName, Action<T> action)
90101

91102
public void Off(string eventName)
92103
{
104+
if (_isDisposed)
105+
{
106+
return;
107+
}
108+
93109
lock (_lockObj)
94110
{
95111
_socket.Off(eventName);
@@ -98,11 +114,33 @@ public void Off(string eventName)
98114

99115
public async Task Emit(string eventName, params object[] args)
100116
{
101-
await _socket.EmitAsync(eventName, args).ConfigureAwait(false);
117+
if (!_isDisposed)
118+
{
119+
await _socket.EmitAsync(eventName, args).ConfigureAwait(false);
120+
}
121+
}
122+
123+
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
124+
public void Dispose()
125+
{
126+
Dispose(true);
127+
GC.SuppressFinalize(this);
128+
}
129+
130+
protected virtual void Dispose(bool disposing)
131+
{
132+
if (disposing)
133+
{
134+
_isDisposed = true;
135+
_socket.Dispose();
136+
}
102137
}
103138

104-
public void DisposeSocket()
139+
private void CheckDisposed()
105140
{
106-
_socket.Dispose();
141+
if (this._isDisposed)
142+
{
143+
throw new ObjectDisposedException(nameof(SocketIoFacade));
144+
}
107145
}
108146
}

src/ElectronNET.API/Runtime/Services/SocketBridge/SocketBridgeService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ protected override Task StartCore()
3333

3434
protected override Task StopCore()
3535
{
36-
this.socket.DisposeSocket();
36+
this.socket.Dispose();
3737
return Task.CompletedTask;
3838
}
3939

src/ElectronNET.IntegrationTests/Tests/BrowserWindowTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,9 +266,11 @@ public async Task BoundsChanged_event_fires_with_updated_bounds()
266266
try
267267
{
268268
window = await Electron.WindowManager.CreateWindowAsync(
269-
new BrowserWindowOptions { Show = false, Width = 300, Height = 200 },
269+
new BrowserWindowOptions { Show = true, Width = 300, Height = 200 },
270270
"about:blank");
271271

272+
await Task.Delay(5.seconds());
273+
272274
var tcs = new TaskCompletionSource<Rectangle>(TaskCreationOptions.RunContinuationsAsynchronously);
273275
window.OnBoundsChanged += bounds => tcs.TrySetResult(bounds);
274276

src/ElectronNET.IntegrationTests/Tests/WebContentsTests.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,15 @@ public async Task GetSetUserAgent_check()
174174

175175
try
176176
{
177+
await Task.Delay(1.seconds());
178+
177179
window = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions { Show = true }, "about:blank");
178180

179-
await Task.Delay(3.seconds());
181+
await Task.Delay(5.seconds());
180182

181183
window.WebContents.SetUserAgent("MyUserAgent/1.0");
182184

183-
await Task.Delay(1.seconds());
185+
await Task.Delay(2.seconds());
184186

185187
var ok = await window.WebContents.GetUserAgentAsync();
186188
ok.Should().Be("MyUserAgent/1.0");

0 commit comments

Comments
 (0)