Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
StackExchange.Redis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
StackExchange.Redis
Commits
21061c57
Commit
21061c57
authored
Jul 17, 2018
by
Marc Gravell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
better cleanup of socket / pipe
parent
babd03cb
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
36 deletions
+46
-36
PhysicalConnection.cs
...kExchange.Redis/StackExchange/Redis/PhysicalConnection.cs
+46
-36
No files found.
StackExchange.Redis/StackExchange/Redis/PhysicalConnection.cs
View file @
21061c57
...
...
@@ -73,7 +73,6 @@ private static readonly Message
private
IDuplexPipe
_ioPipe
;
private
Socket
_socket
;
private
SocketAsyncEventArgs
_socketArgs
;
public
PhysicalConnection
(
PhysicalBridge
bridge
)
{
...
...
@@ -105,25 +104,28 @@ internal async void BeginConnectAsync(TextWriter log)
bridge
.
Multiplexer
.
LogLocked
(
log
,
"BeginConnect: {0}"
,
Format
.
ToString
(
endpoint
));
var
awaitable
=
new
SocketAwaitable
();
_socketArgs
=
new
SocketAsyncEventArgs
{
UserToken
=
awaitable
,
RemoteEndPoint
=
endpoint
,
};
_socketArgs
.
Completed
+=
SocketAwaitable
.
Callback
;
CancellationTokenSource
timeoutSource
=
null
;
try
{
if
(
_socket
.
ConnectAsync
(
_socketArgs
))
{
// asynchronous operation is pending
timeoutSource
=
ConfigureTimeout
(
_socketArgs
,
bridge
.
Multiplexer
.
RawConfig
.
ConnectTimeout
);
}
else
{
// completed synchronously
SocketAwaitable
.
OnCompleted
(
_socketArgs
);
var
awaitable
=
new
SocketAwaitable
();
using
(
var
_socketArgs
=
new
SocketAsyncEventArgs
{
UserToken
=
awaitable
,
RemoteEndPoint
=
endpoint
,
})
{
_socketArgs
.
Completed
+=
SocketAwaitable
.
Callback
;
if
(
_socket
.
ConnectAsync
(
_socketArgs
))
{
// asynchronous operation is pending
timeoutSource
=
ConfigureTimeout
(
_socketArgs
,
bridge
.
Multiplexer
.
RawConfig
.
ConnectTimeout
);
}
else
{
// completed synchronously
SocketAwaitable
.
OnCompleted
(
_socketArgs
);
}
}
// Complete connection
try
{
...
...
@@ -132,8 +134,11 @@ internal async void BeginConnectAsync(TextWriter log)
if
(
ignoreConnect
)
return
;
await
awaitable
;
// wait for the connect to complete or fail (will throw)
timeoutSource
?.
Cancel
();
if
(
timeoutSource
!=
null
)
{
timeoutSource
.
Cancel
();
timeoutSource
.
Dispose
();
}
if
(
await
ConnectedAsync
(
_socket
,
log
,
bridge
.
Multiplexer
.
SocketManager
).
ForAwait
())
{
bridge
.
Multiplexer
.
LogLocked
(
log
,
"Starting read"
);
...
...
@@ -181,6 +186,10 @@ internal async void BeginConnectAsync(TextWriter log)
}
throw
;
}
finally
{
if
(
timeoutSource
!=
null
)
try
{
timeoutSource
.
Dispose
();
}
catch
{
}
}
}
private
static
CancellationTokenSource
ConfigureTimeout
(
SocketAsyncEventArgs
args
,
int
timeoutMilliseconds
)
...
...
@@ -233,23 +242,14 @@ private bool IncludeDetailInExceptions
partial
void
ShouldIgnoreConnect
(
ref
bool
ignore
);
[
System
.
Diagnostics
.
CodeAnalysis
.
SuppressMessage
(
"Microsoft.Usage"
,
"CA2202:Do not dispose objects multiple times"
)]
internal
void
Shutdown
()
internal
void
Shutdown
()
{
var
ioPipe
=
_ioPipe
;
var
socket
=
_socket
;
_ioPipe
=
null
;
socket
=
null
;
if
(
socket
!=
null
)
{
try
{
socket
.
Shutdown
(
SocketShutdown
.
Both
);
}
catch
{
}
try
{
socket
.
Close
();
}
catch
{
}
try
{
socket
.
Dispose
();
}
catch
{
}
}
try
{
using
(
_socketArgs
)
{
}
}
catch
{
}
}
public
void
Dispose
()
{
var
ioPipe
=
_ioPipe
;
_ioPipe
=
null
;
if
(
ioPipe
!=
null
)
{
Trace
(
"Disconnecting..."
);
...
...
@@ -257,18 +257,28 @@ public void Dispose()
try
{
ioPipe
.
Input
?.
Complete
();
}
catch
{
}
try
{
ioPipe
.
Output
?.
CancelPendingFlush
();
}
catch
{
}
try
{
ioPipe
.
Output
?.
Complete
();
}
catch
{
}
ioPipe
.
Output
?.
Complete
();
try
{
using
(
ioPipe
as
IDisposable
)
{
}
}
catch
{
}
}
if
(
socket
!=
null
)
{
try
{
socket
.
Shutdown
(
SocketShutdown
.
Both
);
}
catch
{
}
try
{
socket
.
Close
();
}
catch
{
}
try
{
socket
.
Dispose
();
}
catch
{
}
}
try
{
using
(
ioPipe
as
IDisposable
)
{
}
}
catch
{
}
}
if
(
_socket
!=
null
)
public
void
Dispose
()
{
bool
markDisposed
=
_socket
!=
null
;
Shutdown
();
if
(
markDisposed
)
{
Shutdown
();
Trace
(
"Disconnected"
);
RecordConnectionFailed
(
ConnectionFailureType
.
ConnectionDisposed
);
}
OnCloseEcho
();
GC
.
SuppressFinalize
(
this
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment