Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a memory leak that occurs with keep alive #155

Merged
merged 2 commits into from
Dec 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/KituraNet/HTTP/HTTPServerResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ public class HTTPServerResponse : ServerResponse {
if !keepAlive && !processor.isUpgrade {
processor.close()
}
Monitor.delegate?.finished(request: processor.request, response: self)
}
Monitor.delegate?.finished(request: processor?.request, response: self)
}

/// Begin flushing the buffer
Expand Down
17 changes: 12 additions & 5 deletions Sources/KituraNet/IncomingSocketHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ public class IncomingSocketHandler {
} else {
Log.error("Write to socket (file descriptor \(socket.socketfd)) failed. Error = \(error).")
}

// There was an error writing to the socket, close the socket
writeBuffer.length = 0
writeBufferPosition = 0
prepareToClose()
}

#if os(OSX) || os(iOS) || os(tvOS) || os(watchOS) || GCD_ASYNCH
Expand All @@ -209,7 +214,7 @@ public class IncomingSocketHandler {
#endif
}

if preparingToClose {
if preparingToClose && writeBuffer.length == writeBufferPosition {
close()
}
}
Expand Down Expand Up @@ -277,11 +282,12 @@ public class IncomingSocketHandler {
/// be closed when all the buffered data has been written.
/// Otherwise, immediately close the socket.
public func prepareToClose() {
if writeBuffer.length == writeBufferPosition {
close()
}
else {
if !preparingToClose {
preparingToClose = true

if writeBuffer.length == writeBufferPosition {
close()
}
}
}

Expand All @@ -304,5 +310,6 @@ public class IncomingSocketHandler {
}
processor?.inProgress = false
processor?.keepAliveUntil = 0.0
processor?.close()
}
}