Skip to content

Commit

Permalink
Dispose CancellationTokenSource after cancelled.
Browse files Browse the repository at this point in the history
  • Loading branch information
nobody committed Dec 7, 2023
1 parent 106f47d commit 634dfc4
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 8 deletions.
4 changes: 3 additions & 1 deletion 3rd/DyFetch/Comps/Fetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public string Fetch(string url, List<string> csses, int timeout, int wait)
if (csses != null && csses.Count > 0)
{
var cts = new CancellationTokenSource(span);
WaitForOneOfCsses(csses, w, cts.Token);
var token = cts.Token;
token.Register(() => cts.Dispose());
WaitForOneOfCsses(csses, w, token);
}
if (wait > 0)
{
Expand Down
5 changes: 4 additions & 1 deletion 3rd/Luna/Models/Apis/Components/Web.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,11 @@ public string Post(string url, string text, int timeout)
using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromMilliseconds(timeout);
var token = new CancellationTokenSource(timeout).Token;
var content = new StringContent(text);

var cts = new CancellationTokenSource(timeout);
var token = cts.Token;
token.Register(() => cts.Dispose());
var resp = await client.PostAsync(url, content, token);
return await resp.Content.ReadAsStringAsync();
}
Expand Down
4 changes: 3 additions & 1 deletion Plugins/NeoLua/Models/Apis/Components/Web.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ public string Post(string url, string text, int timeout)
using (var client = new HttpClient())
{
client.Timeout = TimeSpan.FromMilliseconds(timeout);
var token = new CancellationTokenSource(timeout).Token;
var content = new StringContent(text);
var cts = new CancellationTokenSource(timeout);
var token = cts.Token;
token.Register(() => cts.Dispose());
var resp = await client.PostAsync(url, content, token);
return await resp.Content.ReadAsStringAsync();
}
Expand Down
3 changes: 2 additions & 1 deletion V2RayGCon/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.9.0.0")]
[assembly: AssemblyVersion("1.9.0.1")]
[assembly: AssemblyFileVersion("1.0.0.0")]

/*
v1.9.0.1 Dispose CancellationTokenSource after cancelled.
--------------------------------------------------------------------
v1.8.9.7 GUI fully supports socks5 proxy.
Remove Services.Cache.Html().
Expand Down
7 changes: 3 additions & 4 deletions VgcApis/Misc/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,11 +1057,10 @@ public static int GetFreeTcpPort()

public static void DoItLater(Action action, long ms)
{
System.Threading.Timer timer = null;
timer = new System.Threading.Timer(
(_) =>
var timer = new System.Threading.Timer(
(state) =>
{
timer.Dispose();
(state as System.Threading.Timer)?.Dispose();
action();
}
);
Expand Down
59 changes: 59 additions & 0 deletions VgcApisTests/Experiments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace VgcApisTests
{
[TestClass]
public class Experiments
{
public Experiments() { }

[TestMethod]
public void CancellationTokenSourceDisposeTest()
{
var cts = new CancellationTokenSource();
var token = cts.Token;
var mre = new ManualResetEventSlim(false);
var task = Task.Run(() =>
{
Assert.IsFalse(token.IsCancellationRequested);
mre.Wait();
Assert.IsTrue(token.IsCancellationRequested);
});

Thread.Sleep(2000);
cts.Cancel();
cts.Dispose();
mre.Set();
task.Wait();
Assert.IsTrue(token.IsCancellationRequested);
}

[TestMethod]
public void TimedCancellationTokenSourceDisposeTest()
{
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
var mre = new ManualResetEventSlim(false);
var token = cts.Token;
token.Register(() =>
{
cts.Dispose();
mre.Set();
});

var task = Task.Run(() =>
{
Assert.IsFalse(token.IsCancellationRequested);
mre.Wait();
Assert.IsTrue(token.IsCancellationRequested);
});
task.Wait();
Assert.IsTrue(token.IsCancellationRequested);
}
}
}
1 change: 1 addition & 0 deletions VgcApisTests/VgcApisTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Base64Tests.cs" />
<Compile Include="BitStreamTests.cs" />
<Compile Include="LibsTests\TicketPoolTests.cs" />
<Compile Include="Experiments.cs" />
<Compile Include="ReflectionTests.cs" />
<Compile Include="ZipExtensionsTests.cs" />
<Compile Include="StringReadWriterTests.cs" />
Expand Down

0 comments on commit 634dfc4

Please sign in to comment.