Skip to content

Commit 145fbb6

Browse files
committed
fix: remove incorrect raise exception within accept loop
1 parent 3032688 commit 145fbb6

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

src/Suave/ConnectionFacade.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ type ConnectionFacade(connection: Connection, runtime: HttpRuntime, connectionPo
387387
// Set the connection ID on the reader for consistent logging
388388
let mutable readTask : Task<Result<unit,Error>> = null
389389
try
390+
try
390391
reader.init()
391392
readTask <- reader.readLoop()
392393
let! loopRes = this.requestLoop()
@@ -395,6 +396,9 @@ type ConnectionFacade(connection: Connection, runtime: HttpRuntime, connectionPo
395396
| Result.Error err ->
396397
if Globals.verbose then
397398
do Console.WriteLine(sprintf "[Conn:%d] accept: Error: %A" connectionId err)
399+
with ex ->
400+
if Globals.verbose then
401+
do Console.WriteLine(sprintf "[Conn:%d] accept: Exception: %s" connectionId ex.Message)
398402
finally
399403
// First phase: stop reader and transport (this unblocks readTask)
400404
this.shutdown()

src/Suave/Sockets/HttpReader.fs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -314,17 +314,21 @@ type HttpReader(transport : ITransport, pipe: Pipe, cancellationToken: Threading
314314
let mutable reading = true
315315
let mutable result = Ok()
316316
let mutable iterationCount = 0
317-
while running && reading && not(readerCancellationTokenSource.IsCancellationRequested) do
318-
iterationCount <- iterationCount + 1
319-
let! a = this.readMoreData()
320-
match a with
321-
| Ok () -> ()
322-
| Result.Error b as a ->
323-
reading <- false
324-
result <- a
325-
this.stop()
326-
try pipe.Writer.Complete() with _ -> ()
327-
return result
317+
try
318+
while running && reading && not(readerCancellationTokenSource.IsCancellationRequested) do
319+
iterationCount <- iterationCount + 1
320+
let! a = this.readMoreData()
321+
match a with
322+
| Ok () -> ()
323+
| Result.Error b as a ->
324+
reading <- false
325+
result <- a
326+
this.stop()
327+
try pipe.Writer.Complete() with _ -> ()
328+
return result
329+
with ex ->
330+
try pipe.Writer.Complete() with _ -> ()
331+
return Result.Error(Error.ConnectionError ex.Message)
328332
}
329333

330334
// Return readLineBuffer to the ArrayPool when the reader is disposed

src/Suave/Tcp.fs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ type StartedData =
3030
(x.GetStartedListeningElapsedMilliseconds()).ToString() + " ms with binding " + x.binding.ip.ToString() + ":" + x.binding.port.ToString()
3131

3232
/// Stop the TCP listener server
33-
let stopTcp reason (socket : Socket) =
33+
let stopTcp (reason:string) (socket : Socket) =
34+
Console.WriteLine("Stopping TCP server due to {0}", reason)
3435
try
3536
socket.Dispose()
3637
with ex ->
@@ -198,7 +199,7 @@ let runServer maxConcurrentOps bufferSize (binding: SocketBinding) (runtime:Http
198199
let ipAddress = startData.binding.ip.ToString()
199200
let port = startData.binding.port
200201

201-
Console.WriteLine($"Smooth! Suave listener started in {startedListeningMilliseconds} ms with binding {ipAddress}:{port}")
202+
Console.WriteLine($"Smooth! Suave v{Globals.SuaveVersion} listener started in {startedListeningMilliseconds} ms with binding {ipAddress}:{port}")
202203

203204
let remoteBinding (socket : Socket) =
204205
let rep = socket.RemoteEndPoint :?> IPEndPoint
@@ -250,10 +251,8 @@ let runServer maxConcurrentOps bufferSize (binding: SocketBinding) (runtime:Http
250251
// SSL handshake failed, return connection to pool
251252
connectionPool.Push(connection)
252253
with ex ->
253-
// Re-raise if not a cancellation exception
254-
match ex with
255-
| :? OperationCanceledException -> ()
256-
| _ -> raise ex
254+
if Globals.verbose then
255+
Console.WriteLine("TCP server accept exception: {0}", ex)
257256

258257
stopTcp "cancellation requested" listenSocket
259258
with
@@ -262,6 +261,7 @@ let runServer maxConcurrentOps bufferSize (binding: SocketBinding) (runtime:Http
262261
| :? TaskCanceledException ->
263262
stopTcp "The operation was canceled" listenSocket
264263
| ex ->
264+
Console.WriteLine("TCP server runtime exception: {0}", ex)
265265
stopTcp "runtime exception" listenSocket
266266
}))
267267

0 commit comments

Comments
 (0)