Skip to content

Commit

Permalink
[fix] v1.1.2023.0913 修正Dispose循环引用的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Sep 13, 2023
1 parent 658bc8a commit 9f76341
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 123 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish-beta.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v2
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 7.0.x
- name: Restore
run: |
dotnet restore NewLife.Thrift/NewLife.Thrift.csproj
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v2
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 7.0.x
- name: Restore
run: |
dotnet restore NewLife.Thrift/NewLife.Thrift.csproj
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v2
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
dotnet-version: 7.0.x
- name: Build
run: dotnet build -c Release
- name: Test
Expand Down
12 changes: 6 additions & 6 deletions NewLife.Thrift/NewLife.Thrift.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<AssemblyTitle>Thrift序列化</AssemblyTitle>
<Description>Thrift序列化支持库</Description>
<Company>新生命开发团队</Company>
<Copyright>©2002-2022 新生命开发团队</Copyright>
<VersionPrefix>1.0</VersionPrefix>
<Copyright>©2002-2023 新生命开发团队</Copyright>
<VersionPrefix>1.1</VersionPrefix>
<VersionSuffix>$([System.DateTime]::Now.ToString(`yyyy.MMdd`))</VersionSuffix>
<Version>$(VersionPrefix).$(VersionSuffix)</Version>
<FileVersion>$(Version)</FileVersion>
Expand Down Expand Up @@ -40,12 +40,12 @@
</PackageReference>
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='netstandard2.1'">
<PackageReference Include="NewLife.Core" Version="9.1.2022.0501" />
<ItemGroup Condition="'$(TargetFramework)'!='net40'">
<PackageReference Include="NewLife.Core" Version="10.5.2023.801" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'!='netstandard2.1'">
<PackageReference Include="NewLife.Core" Version="8.11.2021.1225" />
<ItemGroup Condition="'$(TargetFramework)'=='net40'">
<PackageReference Include="NewLife.Core" Version="10.5.2023.801-net40" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net45'">
Expand Down
9 changes: 3 additions & 6 deletions NewLife.Thrift/Server/TSimpleServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ public override void Serve()
}

//Fire the preServe server event when server is up but before any client connections
if (serverEventHandler != null)
serverEventHandler.preServe();
serverEventHandler?.preServe();

while (!stop)
{
Expand Down Expand Up @@ -118,8 +117,7 @@ public override void Serve()
//N.B. This is the pattern implemented in C++ and the event fires provisionally.
//That is to say it may be many minutes between the event firing and the client request
//actually arriving or the client may hang up without ever makeing a request.
if (serverEventHandler != null)
serverEventHandler.processContext(connectionContext, inputTransport);
serverEventHandler?.processContext(connectionContext, inputTransport);
//Process client request (blocks until transport is readable)
if (!processor.Process(inputProtocol, outputProtocol))
break;
Expand All @@ -143,8 +141,7 @@ public override void Serve()
}

//Fire deleteContext server event after client disconnects
if (serverEventHandler != null)
serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol);
serverEventHandler?.deleteContext(connectionContext, inputProtocol, outputProtocol);
}
}

Expand Down
27 changes: 9 additions & 18 deletions NewLife.Thrift/Server/TThreadPoolServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ public override void Serve()
}

//Fire the preServe server event when server is up but before any client connections
if (serverEventHandler != null)
serverEventHandler.preServe();
serverEventHandler?.preServe();

while (!stop)
{
Expand Down Expand Up @@ -218,8 +217,7 @@ private void Execute(Object threadContext)
//N.B. This is the pattern implemented in C++ and the event fires provisionally.
//That is to say it may be many minutes between the event firing and the client request
//actually arriving or the client may hang up without ever makeing a request.
if (serverEventHandler != null)
serverEventHandler.processContext(connectionContext, inputTransport);
serverEventHandler?.processContext(connectionContext, inputTransport);
//Process client request (blocks until transport is readable)
if (!processor.Process(inputProtocol, outputProtocol))
break;
Expand All @@ -236,27 +234,20 @@ private void Execute(Object threadContext)
}

//Fire deleteContext server event after client disconnects
if (serverEventHandler != null)
serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol);
serverEventHandler?.deleteContext(connectionContext, inputProtocol, outputProtocol);

}
finally
{
//Close transports
if (inputTransport != null)
inputTransport.Close();
if (outputTransport != null)
outputTransport.Close();
inputTransport?.Close();
outputTransport?.Close();

// disposable stuff should be disposed
if (inputProtocol != null)
inputProtocol.Dispose();
if (outputProtocol != null)
outputProtocol.Dispose();
if (inputTransport != null)
inputTransport.Dispose();
if (outputTransport != null)
outputTransport.Dispose();
inputProtocol?.Dispose();
outputProtocol?.Dispose();
inputTransport?.Dispose();
outputTransport?.Dispose();
}
}
}
Expand Down
21 changes: 7 additions & 14 deletions NewLife.Thrift/Server/TThreadedServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public override void Serve()
}

//Fire the preServe server event when server is up but before any client connections
if (serverEventHandler != null)
serverEventHandler.preServe();
serverEventHandler?.preServe();

while (!stop)
{
Expand Down Expand Up @@ -200,8 +199,7 @@ private void ClientWorker(Object context)
//N.B. This is the pattern implemented in C++ and the event fires provisionally.
//That is to say it may be many minutes between the event firing and the client request
//actually arriving or the client may hang up without ever makeing a request.
if (serverEventHandler != null)
serverEventHandler.processContext(connectionContext, inputTransport);
serverEventHandler?.processContext(connectionContext, inputTransport);
//Process client request (blocks until transport is readable)
if (!processor.Process(inputProtocol, outputProtocol))
break;
Expand All @@ -218,8 +216,7 @@ private void ClientWorker(Object context)
}

//Fire deleteContext server event after client disconnects
if (serverEventHandler != null)
serverEventHandler.deleteContext(connectionContext, inputProtocol, outputProtocol);
serverEventHandler?.deleteContext(connectionContext, inputProtocol, outputProtocol);

lock (clientLock)
{
Expand All @@ -231,16 +228,12 @@ private void ClientWorker(Object context)
finally
{
//Close transports
if (inputTransport != null)
inputTransport.Close();
if (outputTransport != null)
outputTransport.Close();
inputTransport?.Close();
outputTransport?.Close();

// disposable stuff should be disposed
if (inputProtocol != null)
inputProtocol.Dispose();
if (outputProtocol != null)
outputProtocol.Dispose();
inputProtocol?.Dispose();
outputProtocol?.Dispose();
}
}
}
Expand Down
26 changes: 12 additions & 14 deletions NewLife.Thrift/Transport/TBufferedTransport.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.IO;

namespace NewLife.Thrift.Transport
Expand Down Expand Up @@ -143,25 +143,23 @@ protected void CheckNotDisposed()
throw new ObjectDisposedException("TBufferedTransport");
}

#region Ïú»Ù
#region 销毁
protected Boolean _IsDisposed { get; private set; }

/// <summary>Ïú»Ù</summary>
/// <summary>销毁</summary>
protected override void Dispose(Boolean disposing)
{
if (!_IsDisposed)
base.Dispose(disposing);

if (_IsDisposed) return;
_IsDisposed = true;

if (disposing)
{
if (disposing)
{
if (inputBuffer != null)
inputBuffer.Dispose();
if (outputBuffer != null)
outputBuffer.Dispose();
if (transport != null)
transport.Dispose();
}
inputBuffer?.Dispose();
outputBuffer?.Dispose();
transport?.Dispose();
}
_IsDisposed = true;
}
#endregion
}
Expand Down
20 changes: 9 additions & 11 deletions NewLife.Thrift/Transport/TFramedTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,17 @@ private void CheckNotDisposed()
/// <param name="disposing"></param>
protected override void Dispose(Boolean disposing)
{
if (!_IsDisposed)
base.Dispose(disposing);

if (_IsDisposed) return;
_IsDisposed = true;

if (disposing)
{
if (disposing)
{
if (readBuffer != null)
readBuffer.Dispose();
if (writeBuffer != null)
writeBuffer.Dispose();
if (transport != null)
transport.Dispose();
}
readBuffer?.Dispose();
writeBuffer?.Dispose();
transport?.Dispose();
}
_IsDisposed = true;
}
#endregion
}
Expand Down
22 changes: 9 additions & 13 deletions NewLife.Thrift/Transport/THttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,7 @@ internal void UpdateStatusToComplete()
_isCompleted = true; //1. set _iscompleted to true
lock (_locker)
{
if (_evt != null)
{
_evt.Set(); //2. set the event, when it exists
}
_evt?.Set(); //2. set the event, when it exists
}
}

Expand All @@ -394,17 +391,16 @@ internal void NotifyCallbackWhenAvailable()
// IDisposable
protected override void Dispose(Boolean disposing)
{
if (!_IsDisposed)
base.Dispose(disposing);

if (_IsDisposed) return;
_IsDisposed = true;

if (disposing)
{
if (disposing)
{
if (inputStream != null)
inputStream.Dispose();
if (outputStream != null)
outputStream.Dispose();
}
inputStream?.Dispose();
outputStream?.Dispose();
}
_IsDisposed = true;
}
#endregion
}
Expand Down
14 changes: 7 additions & 7 deletions NewLife.Thrift/Transport/TMemoryBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ public static T DeSerialize<T>(Byte[] buf) where T : TAbstractBase
/// <summary>销毁</summary>
protected override void Dispose(Boolean disposing)
{
if (!_IsDisposed)
base.Dispose(disposing);

if (_IsDisposed) return;
_IsDisposed = true;

if (disposing)
{
if (disposing)
{
if (byteStream != null)
byteStream.Dispose();
}
byteStream?.Dispose();
}
_IsDisposed = true;
}
}
}
4 changes: 3 additions & 1 deletion NewLife.Thrift/Transport/TNamedPipeClientTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public override void Write(Byte[] buf, Int32 off, Int32 len)

protected override void Dispose(Boolean disposing)
{
client.Dispose();
base.Dispose(disposing);

client?.Dispose();
}
}
}
8 changes: 4 additions & 4 deletions NewLife.Thrift/Transport/TNamedPipeServerTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ public override void Open()

public override void Close()
{
if (stream != null)
stream.Close();
stream?.Close();
}

public override Int32 Read(Byte[] buf, Int32 off, Int32 len)
Expand Down Expand Up @@ -264,8 +263,9 @@ public override void Write(Byte[] buf, Int32 off, Int32 len)

protected override void Dispose(Boolean disposing)
{
if (stream != null)
stream.Dispose();
base.Dispose(disposing);

stream?.Dispose();
}
}
}
Expand Down
Loading

0 comments on commit 9f76341

Please sign in to comment.