Skip to content

Commit

Permalink
Fix InstanceStream memory leak and remove instance cap
Browse files Browse the repository at this point in the history
  • Loading branch information
hoontee committed Dec 8, 2024
1 parent 55283f5 commit 449d2bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 25 additions & 5 deletions Pronghorn/New.luau
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ local New = {}
-- Dependencies
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

-- Services
local HttpService = game:GetService("HttpService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

-- Core
local Print = require(script.Parent.Debug).Print

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Helper Variables
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -235,8 +239,12 @@ end

--- Creates and returns a `QueuedEvent`.
--- @param nameHint? -- The name of the `QueuedEvent` for debugging.
--- @param queueSize? -- The queue size of the `QueuedEvent`.
--- @return Event<...any> -- The new `QueuedEvent`.
function New.QueuedEvent(nameHint: string?): Event<...any>
function New.QueuedEvent(nameHint: string?, queueSize: number?): Event<...any>
queueSize = queueSize or QUEUED_EVENT_QUEUE_SIZE
assert(queueSize)

local callbacks: {Callback<...any>} = {}
local waiting: {Callback<...any> | thread} = {}
local queueCount = 0
Expand All @@ -260,7 +268,7 @@ function New.QueuedEvent(nameHint: string?): Event<...any>
local actions: Event<...any> = {
Fire = function(self: Event<...any>, ...: any): ()
if not next(callbacks) and not next(waiting) then
if queueCount >= QUEUED_EVENT_QUEUE_SIZE then
if queueCount >= queueSize then
task.spawn(error, `QueuedEvent invocation queue exhausted{if nameHint then ` for '{nameHint}'` else ""}; did you forget to connect to it?`, 0)
end
queueCount += 1
Expand Down Expand Up @@ -420,6 +428,7 @@ function New.ServerInstanceStream(players: Player | {Player}, instances: {Instan
local uid = `{HttpService:GenerateGUID(false)}_{#instances}`
local containers: {[Player]: Instance}? = if exclusive then {} else nil
local clonedInstances: {[Player]: {any}}? = if exclusive and type(players) == "table" then {} else nil
local ancestryListeners: {RBXScriptConnection} = {}

for _, player in (if type(players) == "table" then players else {players}) :: {Player} do
if not player.Parent then continue end
Expand All @@ -428,6 +437,9 @@ function New.ServerInstanceStream(players: Player | {Player}, instances: {Instan
OnServerEvent = function()
if not exclusive then
container:Destroy()
for _, connection in ancestryListeners do
connection:Disconnect()
end
end
end;
})
Expand All @@ -450,11 +462,11 @@ function New.ServerInstanceStream(players: Player | {Player}, instances: {Instan
Children = if exclusive then {instance} else nil;
})

instance.AncestryChanged:Once(function(): ()
table.insert(ancestryListeners, instance.AncestryChanged:Once(function(): ()
if not instance.Parent then
objectValue:SetAttribute("Canceled", true)
end
end)
end))
end

container.Parent = player.PlayerGui
Expand All @@ -481,7 +493,7 @@ function New.ClientInstanceStream(uid: string): (Event<...any?>, Event<any?>, In
local instances: {Instance} = {}
local failedInstances: {true} = {}
local finishedEvent: Event<...Instance?> = New.QueuedEvent("InstanceStream Finished Event")
local streamEvent: Event<Instance?> = New.QueuedEvent("InstanceStream Stream Event")
local streamEvent: Event<Instance?> = New.QueuedEvent("InstanceStream Stream Event", math.huge)

local function checkFinished(): boolean
for index = 1, numInstances do
Expand All @@ -493,6 +505,7 @@ function New.ClientInstanceStream(uid: string): (Event<...any?>, Event<any?>, In
finishedEvent:DisconnectAll()
streamEvent:DisconnectAll()
container:FireServer()
Print(`InstanceStream '{uid}' finished`)
return true
end

Expand All @@ -503,19 +516,24 @@ function New.ClientInstanceStream(uid: string): (Event<...any?>, Event<any?>, In

container.Parent = localPlayer

Print(`InstanceStream '{uid}' starting`)

for _, child in container:GetChildren() do
assert(child:IsA("ObjectValue"))
if child.Value then
Print(`InstanceStream '{uid}' received '{child.Name}': {child.Value:GetFullName()}`)
instances[tonumber(child.Name) :: number] = child.Value
streamEvent:Fire(child.Value)
else
child:GetAttributeChangedSignal("Canceled"):Once(function(): ()
Print(`InstanceStream '{uid}' canceled '{child.Name}'`)
failedInstances[tonumber(child.Name) :: number] = true
streamEvent:Fire(nil)
checkFinished()
end)
child.Changed:Once(function(): ()
assert(child.Value)
Print(`InstanceStream '{uid}' received '{child.Name}': {child.Value:GetFullName()}`)
instances[tonumber(child.Name) :: number] = child.Value
streamEvent:Fire(tonumber(child.Name) :: number, child.Value)
checkFinished()
Expand All @@ -527,12 +545,14 @@ function New.ClientInstanceStream(uid: string): (Event<...any?>, Event<any?>, In
container.ChildAdded:Connect(function(child: Instance): ()
assert(child:IsA("ObjectValue"))
child:GetAttributeChangedSignal("Canceled"):Once(function(): ()
Print(`InstanceStream '{uid}' canceled '{child.Name}'`)
failedInstances[tonumber(child.Name) :: number] = true
streamEvent:Fire(nil)
checkFinished()
end)
child.Changed:Once(function(): ()
assert(child.Value)
Print(`InstanceStream '{uid}' received '{child.Name}': {child.Value:GetFullName()}`)
instances[tonumber(child.Name) :: number] = child.Value
streamEvent:Fire(child)
checkFinished()
Expand Down
2 changes: 1 addition & 1 deletion Pronghorn/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
║ ██████▀██▓▌▀▌ ▄ ▄▓▌▐▓█▌ ║
║ ║
║ ║
║ Pronghorn Framework Rev. B80
║ Pronghorn Framework Rev. B81
║ https://github.com/Iron-Stag-Games/Pronghorn ║
║ GNU Lesser General Public License v2.1 ║
║ ║
Expand Down

0 comments on commit 449d2bb

Please sign in to comment.