diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 241fb6a..46449e1 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -34,6 +34,8 @@ jobs: - uses: julia-actions/cache@v1 - uses: julia-actions/julia-buildpkg@v1 - uses: julia-actions/julia-runtest@v1 + env: + JULIA_NUM_THREADS: 4 # the GC-race test needs other threads to collect during a detection - uses: julia-actions/julia-processcoverage@v1 - uses: codecov/codecov-action@v2 with: diff --git a/src/helpers.jl b/src/helpers.jl index 4d19081..454eac5 100644 --- a/src/helpers.jl +++ b/src/helpers.jl @@ -144,11 +144,10 @@ function (detector::AprilTagDetector)(image::Array{T, 2}) where T <: U8Types if detector.tf == C_NULL error("AprilTags family does not exist") end - #create image8 object for april tags - image8 = convert(AprilTags.image_u8_t, image) - - # run detector on image - detections = apriltag_detector_detect(detector.td, image8) + # run detector on image, its pixels kept alive for the whole call + detections = with_image_u8(image) do image8 + apriltag_detector_detect(detector.td, image8) + end # copy and return detections julia struct tags = AprilTags.copyAprilTagDetections(detections) @@ -182,11 +181,10 @@ function threadcalldetect(detector::AprilTagDetector, image::Array{T, 2}) where if detector.tf == C_NULL error("AprilTags family does not exist") end - #create image8 object for april tags - image8 = convert(AprilTags.image_u8_t, image) - - # run detector on image - detections = threadcall_apriltag_detector_detect(detector.td, image8) + # run detector on image, its pixels kept alive for the whole call + detections = with_image_u8(image) do image8 + threadcall_apriltag_detector_detect(detector.td, image8) + end # copy and return detections julia struct tags = AprilTags.copyAprilTagDetections(detections) @@ -234,6 +232,23 @@ end # imbuf = reinterpret(UInt8, image'[:]) # return AprilTags.image_u8_t(Int32(cols), Int32(rows), Int32(cols), Base.unsafe_convert(Ptr{UInt8}, imbuf)) # end +""" + with_image_u8(f, image) + +Call `f(image8)` with `image8::image_u8_t`, a C view of `image`'s pixels, and keep those pixels +alive until `f` returns. `image8` points into a row-major copy of `image` that nothing else +references, so a garbage collection while C reads it would free it under the detector (on +another thread, during a long `apriltag_detector_detect`): `GC.@preserve` prevents that. +`image8` must not outlive `f`. +""" +function with_image_u8(f, image::AbstractMatrix{T}) where T <: U8Types + (rows, cols) = size(image) + imbuf = reinterpret(UInt8, vec(permutedims(image))) + return GC.@preserve imbuf f(image_u8_t(Int32(cols), Int32(rows), Int32(cols), Base.unsafe_convert(Ptr{UInt8}, imbuf))) +end + +# Unsafe: the returned `image_u8_t` points into a copy of `image` that nothing keeps alive, so +# the pointer can dangle as soon as this returns. Detection goes through `with_image_u8`. function convert(::Type{image_u8_t}, image::Array{UInt8, 2}) #create image8 opject for april tags (rows,cols) = size(image) @@ -662,11 +677,10 @@ function detectAndPose( detector::AprilTagDetector, if detector.tf == C_NULL error("AprilTags family does not exist") end - #create image8 object for april tags - image8 = convert(AprilTags.image_u8_t, image) - - # run detector on image - detections = apriltag_detector_detect(detector.td, image8) + # run detector on image, its pixels kept alive for the whole call + detections = with_image_u8(image) do image8 + apriltag_detector_detect(detector.td, image8) + end # copy and return detections julia struct tags = AprilTags.copyAprilTagDetections(detections) diff --git a/test/basictests.jl b/test/basictests.jl index 5f7ad7f..6a0b22c 100644 --- a/test/basictests.jl +++ b/test/basictests.jl @@ -278,4 +278,37 @@ using Test @test length(tags) == 1 freeDetector!(detector) end + + # The detector reads a row-major copy of the image through a raw pointer. Unless that copy is + # kept alive, a garbage collection on another thread during the detection frees it: tags go + # missing, or the process segfaults on unmapped memory. Other threads allocating and collecting + # provoke it. With one thread nothing can collect during the C call, so this needs + # JULIA_NUM_THREADS > 1 to test anything. + @testset "Detection survives garbage collection on other threads" begin + scene = fill(0xff, 480, 640) + for (id, (r, c)) in enumerate([(50, 50), (150, 250), (250, 450)]) + tag = kron(reinterpret(UInt8, getAprilTagImage(id - 1)), ones(UInt8, 3, 3)) + scene[r .+ axes(tag, 1), c .+ axes(tag, 2)] .= tag + end + scene = Gray.(reinterpret(N0f8, scene)) + detector = AprilTagDetector() + stop = Threads.Atomic{Bool}(false) + churn = map(1:8) do _ + Threads.@spawn while !stop[] + # several sizes: which one lands on the freed copy depends on the allocator + for bytes in (100_000, 300_000, 1_000_000) + fill!(Vector{UInt8}(undef, bytes), 0x01) + GC.gc(false) + end + end + end + found = try + [length(detector(scene)) for _ in 1:300] + finally + stop[] = true + foreach(wait, churn) + freeDetector!(detector) + end + @test all(==(3), found) + end end