resourcepool_free should remove the resource from the request resource cache

Fri, 10 Mar 2023 23:00:02 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 10 Mar 2023 23:00:02 +0100
changeset 460
b9a447b02046
parent 459
f21b4ff81c01
child 461
9b20b8f3582b

resourcepool_free should remove the resource from the request resource cache

src/server/daemon/httprequest.c file | annotate | diff | comparison | revisions
src/server/daemon/request.h file | annotate | diff | comparison | revisions
src/server/daemon/resourcepool.c file | annotate | diff | comparison | revisions
src/server/daemon/resourcepool.h file | annotate | diff | comparison | revisions
--- a/src/server/daemon/httprequest.c	Fri Mar 10 22:45:58 2023 +0100
+++ b/src/server/daemon/httprequest.c	Fri Mar 10 23:00:02 2023 +0100
@@ -601,6 +601,7 @@
 }
 
 int nsapi_finish_request(NSAPISession *sn, NSAPIRequest *rq) {
+    rq->finished = TRUE;
     request_free_resources(sn, rq);
     
     WSBool read_stream_eof = httpstream_eof(sn->sn.csd);
--- a/src/server/daemon/request.h	Fri Mar 10 22:45:58 2023 +0100
+++ b/src/server/daemon/request.h	Fri Mar 10 23:00:02 2023 +0100
@@ -49,6 +49,7 @@
     NSAPIContext   context;
     void           *jvm_context;
     CxMap          *resources;
+    WSBool         finished;
 };
 
 /* macros for context access */
--- a/src/server/daemon/resourcepool.c	Fri Mar 10 22:45:58 2023 +0100
+++ b/src/server/daemon/resourcepool.c	Fri Mar 10 23:00:02 2023 +0100
@@ -60,6 +60,12 @@
         return 1;
     }
     
+    char *respool_name = cx_strdup_a(cfg->a, name).ptr;
+    if(!respool_name) {
+        log_ereport(LOG_FAILURE, "resourcepool_new: OOM");
+        return 1;
+    }
+    
     // convert ConfigNode to pblock
     // no sub-objects allowed for this specific ConfigNode, therefore
     // it can be represented as key-value-pairs
@@ -82,6 +88,7 @@
         return 1;
     }
     
+    respool->name = respool_name;
     respool->type = restype;
     respool->data = respool_data;
     respool->min = 4; // TODO: get from node
@@ -211,9 +218,18 @@
 }
 
 void resourcepool_free(Session *sn, Request *rq, ResourceData *resource) {
+    NSAPIRequest *nsapi_rq = (NSAPIRequest *)rq;
     ResourceDataPrivate *res = (ResourceDataPrivate*)resource;
     ResourcePool *respool = resource->resourcepool;
     
+    if(nsapi_rq && !nsapi_rq->finished) {
+        // request processing still ongoing and SAFs will be executed
+        if(!cxMapRemove(nsapi_rq->resources, cx_hash_key_str(respool->name))) {
+            log_ereport(LOG_FAILURE, "resourcepool_free: cannot remove resource from request: potential double free");
+        }
+    }
+    // else: safe to ignore nsapi_rq->resources
+    
     if(respool->type->finish(respool->data, res->resdata)) {
         log_ereport(LOG_FAILURE, "resourcepool_free: finish failed");
     }
--- a/src/server/daemon/resourcepool.h	Fri Mar 10 22:45:58 2023 +0100
+++ b/src/server/daemon/resourcepool.h	Fri Mar 10 23:00:02 2023 +0100
@@ -67,6 +67,11 @@
     ResourceType *type;
     
     /*
+     * The name of the resource pool
+     */
+    char *name;
+    
+    /*
      * Data returned by the ResourceType init function
      * When the pool is destroyed, the data should be passed to
      * ResourceType.destroy

mercurial