Browse Source

Changes Unpin() logic as suggested by @withinboredom

Alliballibaba 3 months ago
parent
commit
ef1bd0d975
5 changed files with 7 additions and 6 deletions
  1. 0 2
      cgi.go
  2. 1 1
      frankenphp.c
  3. 3 0
      phpthread.go
  4. 0 1
      thread-regular.go
  5. 3 2
      thread-worker.go

+ 0 - 2
cgi.go

@@ -227,8 +227,6 @@ func go_frankenphp_release_known_variable_keys(threadIndex C.uintptr_t) {
 	for _, v := range thread.knownVariableKeys {
 		C.frankenphp_release_zend_string(v)
 	}
-	// release everything that might still be pinned to the thread
-	thread.Unpin()
 	thread.knownVariableKeys = nil
 }
 

+ 1 - 1
frankenphp.c

@@ -89,7 +89,7 @@ static void frankenphp_free_request_context() {
   free(ctx->cookie_data);
   ctx->cookie_data = NULL;
 
-  /* Is freed via thread.Unpin() at the end of each request */
+  /* Is freed via thread.Unpin() */
   SG(request_info).auth_password = NULL;
   SG(request_info).auth_user = NULL;
   SG(request_info).request_method = NULL;

+ 3 - 0
phpthread.go

@@ -102,10 +102,13 @@ func go_frankenphp_after_script_execution(threadIndex C.uintptr_t, exitStatus C.
 		panic(ScriptExecutionError)
 	}
 	thread.handler.afterScriptExecution(int(exitStatus))
+
+	// unpin all memory used during script execution
 	thread.Unpin()
 }
 
 //export go_frankenphp_on_thread_shutdown
 func go_frankenphp_on_thread_shutdown(threadIndex C.uintptr_t) {
+	phpThreads[threadIndex].Unpin()
 	phpThreads[threadIndex].state.set(stateDone)
 }

+ 0 - 1
thread-regular.go

@@ -61,7 +61,6 @@ func (handler *regularThread) waitForRequest() string {
 		if err := updateServerContext(handler.thread, r, true, false); err != nil {
 			rejectRequest(fc.responseWriter, err.Error())
 			handler.afterRequest(0)
-			handler.thread.Unpin()
 			// go back to beforeScriptExecution
 			return handler.beforeScriptExecution()
 		}

+ 3 - 2
thread-worker.go

@@ -140,6 +140,9 @@ func tearDownWorkerScript(handler *workerThread, exitStatus int) {
 }
 
 func (handler *workerThread) waitForWorkerRequest() bool {
+	// unpin any memory left over from previous requests
+	handler.thread.Unpin()
+
 	if c := logger.Check(zapcore.DebugLevel, "waiting for request"); c != nil {
 		c.Write(zap.String("worker", handler.worker.fileName))
 	}
@@ -180,7 +183,6 @@ func (handler *workerThread) waitForWorkerRequest() bool {
 		rejectRequest(fc.responseWriter, err.Error())
 		maybeCloseContext(fc)
 		handler.workerRequest = nil
-		handler.thread.Unpin()
 
 		return handler.waitForWorkerRequest()
 	}
@@ -201,7 +203,6 @@ func go_frankenphp_finish_worker_request(threadIndex C.uintptr_t) {
 
 	maybeCloseContext(fc)
 	thread.handler.(*workerThread).workerRequest = nil
-	thread.Unpin()
 
 	if c := fc.logger.Check(zapcore.DebugLevel, "request handling finished"); c != nil {
 		c.Write(zap.String("worker", fc.scriptFilename), zap.String("url", r.RequestURI))