Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ServiceWorker release references to completed tasks. Fixes #854 #869

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions PushSharp.Core/ServiceBroker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ public void Start ()

// Let's wait for the continuation not the task itself
toSend.Add (cont);

// release references to completed tasks
toSend.RemoveAll(task => task.IsCompleted);
Copy link

@houseofcat houseofcat Apr 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got pinged from a comment I opened 4 years ago.

This should help but I believe you want to check if Task.IsFaulted (as well) GC will throw a UnobservedTaskException if the error is not handled. Or you could add an AppDomain UnobservedTaskException handler.

https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.iscompleted?view=net-5.0

Completed => RanToCompletion, Faulted, or Canceled

}

if (toSend.Count <= 0)
Expand Down
22 changes: 15 additions & 7 deletions PushSharp.Tests/BrokerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,21 @@ public void Broker_Send_Many ()
var succeeded = 0;
var failed = 0;
var attempted = 0;

var locker = new object();

var broker = new TestServiceBroker ();
broker.OnNotificationFailed += (notification, exception) => {
failed++;
lock (locker)
{
failed++;
}
};
broker.OnNotificationSucceeded += (notification) => {
succeeded++;
broker.OnNotificationSucceeded += (notification) =>
{
lock (locker)
{
succeeded++;
}
};
broker.Start ();
broker.ChangeScale (1);
Expand All @@ -38,10 +46,10 @@ public void Broker_Send_Many ()
for (int i = 1; i <= 1000; i++) {
attempted++;
broker.QueueNotification (new TestNotification { TestId = i });
}

}
broker.Stop ();

c.StopAndLog ("Test Took {0} ms");

Assert.AreEqual (attempted, succeeded);
Expand Down