Skip to content

Turning on *resource-debug-double-free* makes every stack/track call throw ClassCastException #4

@willcohen

Description

@willcohen

One liner repro:
clj -Sdeps '{:deps {techascent/tech.resource {:mvn/version "5.09"}}}' -M -e "(require (quote [tech.v3.resource.stack :as s])) (alter-var-root (var s/*resource-debug-double-free*) (constantly true)) (s/track (Object.) (fn []))"

Output:

true
Execution error (ClassCastException) at tech.v3.resource.stack/track (stack.clj:46).
class java.util.ArrayList cannot be cast to class java.util.concurrent.Future (java.util.ArrayList and java.util.concurrent.Future are in module java.base of loader 'bootstrap')

Potential fix:

--- a/src/tech/v3/resource/stack.clj
+++ b/src/tech/v3/resource/stack.clj
@@ -43,7 +43,7 @@
   itself.  Else the dispose function is tracked."
   ([item dispose-fn]
    (when (and *resource-debug-double-free*
-              (some #(identical? item %) @*resource-context*))
+              (some #(identical? item (first %)) *resource-context*))
      (throw (ex-info "Duplicate track detected; this will result in a double free"
                      {:item item})))
    (when-not *bound-resource-context?*

When change noted above is applied
clj -Sdeps '{:deps {techascent/tech.resource {:local/root "/path/to/local/tech.resource"}}}' -M -e "(require (quote [tech.v3.resource.stack :as s])) (alter-var-root (var s/*resource-debug-double-free*) (constantly true)) (let [o (Object.)] (s/track o (fn [])) (println \"first track OK\") (s/track o (fn [])))"

Output:

true
Apr 17, 2026 10:10:08 PM clojure.tools.logging$eval449$fn__452 invoke
WARNING: Stack resource tracking used but no resource context bound.
This is probably a memory leak.
first track OK
Execution error (ExceptionInfo) at tech.v3.resource.stack/track (stack.clj:47).
Duplicate track detected; this will result in a double free

Is this flag meant to still be supported?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions