断开连接时出现IdHTTP访问冲突

Mik*_*vis 5 delphi authentication indy disconnect idhttp

Indy 10:

两个线程,线程1在TIdHTTP上调用Get并阻止读取数据.线程2将在同一TIdHTTP上调用disconnect以中断Get.

我在TIdHTTP上使用摘要式身份验证,我偶尔会得到一个AV.

线程1的callstack:

40491D [system.pas][System][@ClassDestroy][8989]
69EF2B [..\..\common\IdAuthenticationDigest.pas][IdAuthenticationDigest][TIdDigestAuthentication.Destroy][109]
404567 [system.pas][System][TObject.Free][8313]
6A2B69 [..\..\Common\IdHTTP.pas][IdHTTP][TIdCustomHTTP.DoOnDisconnected][1587]
534574 [IdTCPConnection.pas][IdTCPConnection][TIdTCPConnection.Disconnect][532]
534B3B [IdTCPConnection.pas][IdTCPConnection][TIdTCPConnection.Disconnect][767]
6A3FB3 [..\..\Common\IdHTTP.pas][IdHTTP][TIdCustomHTTP.DoRequest][2101]
Run Code Online (Sandbox Code Playgroud)

线程2的callstack:

402BA3 [system.pas][System][@FreeMem][2477]
404521 [system.pas][System][TObject.FreeInstance][8294]
40491D [system.pas][System][@ClassDestroy][8989]
69EF2B [..\..\common\IdAuthenticationDigest.pas][IdAuthenticationDigest]        [TIdDigestAuthentication.Destroy][109]
404567 [system.pas][System][TObject.Free][8313]
6A2B69 [..\..\Common\IdHTTP.pas][IdHTTP][TIdCustomHTTP.DoOnDisconnected][1587]
534574 [IdTCPConnection.pas][IdTCPConnection][TIdTCPConnection.Disconnect][532]
534B3B [IdTCPConnection.pas][IdTCPConnection][TIdTCPConnection.Disconnect][767]
Run Code Online (Sandbox Code Playgroud)

基本上在DoRequest结束时它会断开连接.似乎在尝试释放Request.Authentication的断开连接上存在竞争条件.

下载了Indy 10的最新代码并查看代码,我相信行为应该是相同的.

我相信从另一个线程断开调用是推荐的使用模式,我错了吗?这是Indy的一个错误吗?似乎有必要锁定断开连接,但很难看到可能造成的死锁.还有其他人遇到过这个吗?

Rem*_*eau 6

是的,您遇到的是竞争条件,Request.Authentication当断开套接字时,两个线程都试图释放相同的对象两次.给定堆栈跟踪,两个线程必须在完全相同的时间断开套接字,因为Disconnect()只有DoOnDisconnect()IOHandler仍然打开时调用,并且在调用IOHandler之前关闭DoOnDisconnect().

你可以尝试做的是使用OnStatus事件进入线程安全锁,例如状态中的临界区或互斥锁hsDisconnecting,并将锁保持在hsDisconnected状态. IOHandler.Close()并且DoOnDisconnect()在这些状态之间调用,这样就可以有效地序列化你的线程,这样它们就不能再断开套接字并Request.Authentication在同一时间释放对象了.