-
-
Notifications
You must be signed in to change notification settings - Fork 280
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 memory leak #442
Merged
Merged
Fix memory leak #442
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2b27f91
do not use caddy context
withinboredom a2238e7
ensure all handles are cleaned up
withinboredom 332baac
do not export types
withinboredom d8e6168
just panic when double deleting a handle
withinboredom fbb51c2
set the minimal capacity
withinboredom 12c14e6
remove micro-opt
withinboredom d5704ad
move handle cleanup to just before we return from serveHttp
withinboredom 3ad75d5
ensure we clean up cli scripts
withinboredom 97a0bb5
handle cgi-mode free
withinboredom 191d15e
micro-optimization: set initial capacity
withinboredom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,9 +52,11 @@ import ( | |
//_ "github.com/ianlancetaylor/cgosymbolizer" | ||
) | ||
|
||
type key int | ||
type contextKeyStruct struct{} | ||
type handleKeyStruct struct{} | ||
|
||
var contextKey key | ||
var contextKey = contextKeyStruct{} | ||
var handleKey = handleKeyStruct{} | ||
|
||
var ( | ||
InvalidRequestError = errors.New("not a FrankenPHP request") | ||
|
@@ -191,7 +193,10 @@ func NewRequestWithContext(r *http.Request, opts ...RequestOption) (*http.Reques | |
// SCRIPT_FILENAME is the absolute path of SCRIPT_NAME | ||
fc.scriptFilename = sanitizedPathJoin(fc.documentRoot, fc.scriptName) | ||
|
||
return r.WithContext(context.WithValue(r.Context(), contextKey, fc)), nil | ||
c := context.WithValue(r.Context(), contextKey, fc) | ||
c = context.WithValue(c, handleKey, Handles()) | ||
|
||
return r.WithContext(c), nil | ||
} | ||
|
||
// FromContext extracts the FrankenPHPContext from a context. | ||
|
@@ -402,9 +407,12 @@ func updateServerContext(request *http.Request, create bool, mrh C.uintptr_t) er | |
|
||
var rh cgo.Handle | ||
if fc.responseWriter == nil { | ||
mrh = C.uintptr_t(cgo.NewHandle(request)) | ||
h := cgo.NewHandle(request) | ||
request.Context().Value(handleKey).(*HandleList).AddHandle(h) | ||
mrh = C.uintptr_t(h) | ||
} else { | ||
rh = cgo.NewHandle(request) | ||
request.Context().Value(handleKey).(*HandleList).AddHandle(rh) | ||
} | ||
|
||
ret := C.frankenphp_update_server_context( | ||
|
@@ -467,7 +475,9 @@ func go_fetch_request() C.uintptr_t { | |
return 0 | ||
|
||
case r := <-requestChan: | ||
return C.uintptr_t(cgo.NewHandle(r)) | ||
h := cgo.NewHandle(r) | ||
r.Context().Value(handleKey).(*HandleList).AddHandle(h) | ||
return C.uintptr_t(h) | ||
} | ||
} | ||
|
||
|
@@ -480,7 +490,6 @@ func maybeCloseContext(fc *FrankenPHPContext) { | |
//export go_execute_script | ||
func go_execute_script(rh unsafe.Pointer) { | ||
handle := cgo.Handle(rh) | ||
defer handle.Delete() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is now deleted by the handle list. |
||
|
||
request := handle.Value().(*http.Request) | ||
fc, ok := FromContext(request.Context()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package frankenphp | ||
|
||
// #include <stdlib.h> | ||
import "C" | ||
import ( | ||
"runtime/cgo" | ||
"unsafe" | ||
) | ||
|
||
/* | ||
FrankenPHP is fairly complex because it shuffles handles/requests/contexts | ||
between C and Go. This simplifies the lifecycle management of per-request | ||
structures by allowing us to hold references until the end of the request | ||
and ensure they are always cleaned up. | ||
*/ | ||
|
||
// PointerList A list of pointers that can be freed at a later time | ||
type PointerList struct { | ||
withinboredom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Pointers []unsafe.Pointer | ||
} | ||
|
||
// HandleList A list of pointers that can be freed at a later time | ||
type HandleList struct { | ||
Handles []cgo.Handle | ||
} | ||
|
||
// AddHandle Call when registering a handle for the very first time | ||
func (h *HandleList) AddHandle(handle cgo.Handle) { | ||
h.Handles = append(h.Handles, handle) | ||
} | ||
|
||
// AddPointer Call when creating a request-level C pointer for the very first time | ||
func (p *PointerList) AddPointer(ptr unsafe.Pointer) { | ||
p.Pointers = append(p.Pointers, ptr) | ||
} | ||
|
||
// FreeAll frees all C pointers | ||
func (p *PointerList) FreeAll() { | ||
for _, ptr := range p.Pointers { | ||
C.free(ptr) | ||
} | ||
p.Pointers = nil // To avoid dangling pointers | ||
} | ||
|
||
// FreeAll frees all handles | ||
func (h *HandleList) FreeAll() { | ||
defer func() { | ||
if err := recover(); err != nil { | ||
withinboredom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
getLogger().Warn("A handle was already deleted manually, indeterminate state") | ||
} | ||
}() | ||
for _, p := range h.Handles { | ||
p.Delete() | ||
} | ||
} | ||
|
||
// Pointers Get a new list of pointers | ||
func Pointers() *PointerList { | ||
return &PointerList{ | ||
Pointers: make([]unsafe.Pointer, 0), | ||
} | ||
} | ||
|
||
// Handles Get a new list of handles | ||
func Handles() *HandleList { | ||
return &HandleList{ | ||
Handles: make([]cgo.Handle, 0), | ||
withinboredom marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed
contextKey
to follow Go best-practices and ensure uniqueness during runtime.