From a13098494d59fb1e6f8cc980a12f408f58a60727 Mon Sep 17 00:00:00 2001 From: Christopher Baines Date: Mon, 17 Nov 2025 11:18:23 +0000 Subject: [PATCH] Fix a bug where resources pools could empty with waiters --- knots/resource-pool.scm | 4 ++++ tests/resource-pool.scm | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/knots/resource-pool.scm b/knots/resource-pool.scm index 232df77..d802260 100644 --- a/knots/resource-pool.scm +++ b/knots/resource-pool.scm @@ -939,6 +939,10 @@ (eq? x resource)) resources))) + (when (and (not (q-empty? waiters)) + (< (- (length resources) 1) + max-size)) + (spawn-fiber-to-return-new-resource)) (loop (if index (remove-at-index! resources index) diff --git a/tests/resource-pool.scm b/tests/resource-pool.scm index 461d04b..3999dde 100644 --- a/tests/resource-pool.scm +++ b/tests/resource-pool.scm @@ -211,4 +211,21 @@ (destroy-resource-pool resource-pool)))) +(run-fibers-for-tests + (lambda () + (let ((resource-pool (make-resource-pool + (const 'foo) + 1 + #:lifetime 1 + #:destructor + (const #t)))) + (for-each + (lambda _ + (with-resource-from-pool resource-pool + res + res)) + (iota 20)) + + (destroy-resource-pool resource-pool)))) + (display "resource-pool test finished successfully\n")