Skip to content

Commit 3d00ce8

Browse files
authored
fix DynamicPPL.jl/pull/261 (#86)
* fix TuringLang/DynamicPPL.jl#261 (comment) * add testcase
1 parent 1c575b7 commit 3d00ce8

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

src/tarray.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ for i in 1:4 ta[i] = i end # assign
3131
Array(ta) # convert to 4-element Array{Int64,1}: [1, 2, 3, 4]
3232
```
3333
"""
34-
struct TArray{T, N, A <: AbstractArray{T, N}} <: AbstractArray{T, N}
34+
mutable struct TArray{T, N, A <: AbstractArray{T, N}} <: AbstractArray{T, N}
3535
orig_task :: Task
3636
data::Dict{Task, Tuple{Int, A}}
3737
function TArray{T, N, A}() where {T, N, A <: AbstractArray{T, N}}
@@ -61,14 +61,13 @@ end
6161
TArray(x::AbstractArray) = convert(TArray, x)
6262

6363
# TArray House-Keeper
64-
6564
const TArrayKeeper = Vector{WeakRef}()
6665
register_to_keeper(x::TArray) = push!(TArrayKeeper, WeakRef(x))
6766
function copy_tarrays(task1::Task, task2::Task)
6867
filter!(x -> x.value !== nothing, TArrayKeeper)
6968
for wref in TArrayKeeper
7069
ta = wref.value
71-
if haskey(ta.data, task1) && !haskey(ta.data, task2)
70+
if ta !== nothing && haskey(ta.data, task1) && !haskey(ta.data, task2)
7271
ta.data[task2] = ta.data[task1]
7372
end
7473
end

test/tarray.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,33 @@
165165
@test DATA[a.task] == [hash(ctask.task), hash(ctask.task), hash(a.task),
166166
hash(a.task)]
167167
end
168+
169+
@testset "Issue: PR-86 (DynamicPPL.jl/pull/261)" begin
170+
function f()
171+
t = TArray(Int, 1)
172+
t[1] = 0
173+
while true
174+
produce(t[1])
175+
t[1]
176+
t[1] = 1 + t[1]
177+
end
178+
end
179+
180+
181+
ctask = CTask(f)
182+
183+
ex = try
184+
for _ in 1:10000
185+
consume(ctask)
186+
consume(ctask)
187+
a = copy(ctask)
188+
consume(a)
189+
consume(a)
190+
end
191+
catch ex
192+
ex
193+
end
194+
@test ex === nothing
195+
end
196+
168197
end

0 commit comments

Comments
 (0)