Skip to content

Commit 3331ab9

Browse files
DrJKLampcode-com
andcommitted
fix: harden subgraph disconnect cleanup for stale links
Amp-Thread-ID: https://ampcode.com/threads/T-019c9bb5-a77c-72ef-9b51-5394c20ee671 Co-authored-by: Amp <amp@ampcode.com>
1 parent 7d737a5 commit 3331ab9

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

src/lib/litegraph/src/subgraph/SubgraphIO.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,35 @@ describe('SubgraphIO - Input Slot Dual-Nature Behavior', () => {
323323
expect(internalNode.onConnectionsChange).toHaveBeenCalled()
324324
})
325325

326+
subgraphTest(
327+
'disconnects subgraph-input links even when link origin_slot points to a missing slot',
328+
({ subgraphWithNode }) => {
329+
const { subgraph } = subgraphWithNode
330+
const internalNode = new LGraphNode('Internal Target')
331+
internalNode.addInput('in', '*')
332+
subgraph.add(internalNode)
333+
334+
const subgraphInput = subgraph.inputNode.slots[0]
335+
const link = subgraphInput.connect(internalNode.inputs[0], internalNode)
336+
if (!link) throw new Error('Expected link')
337+
338+
// Simulate stale legacy/corrupt topology where the slot index no longer resolves.
339+
link.origin_slot = 999
340+
341+
new ToInputFromIoNodeLink(
342+
subgraph,
343+
subgraph.inputNode,
344+
subgraphInput,
345+
undefined,
346+
LinkDirection.CENTER,
347+
link
348+
).disconnect()
349+
350+
expect(internalNode.inputs[0].link).toBeNull()
351+
expect(subgraph.links.get(link.id)).toBeUndefined()
352+
}
353+
)
354+
326355
subgraphTest(
327356
'handles slot renaming with active connections',
328357
({ subgraphWithNode }) => {
@@ -509,6 +538,31 @@ describe('SubgraphIO - Output Slot Dual-Nature Behavior', () => {
509538
expect(subgraph.outputs[0].displayName).toBe('new_name')
510539
}
511540
)
541+
542+
subgraphTest(
543+
'cleans stale subgraph-output linkIds while disconnecting active output links',
544+
({ subgraphWithNode }) => {
545+
const { subgraph } = subgraphWithNode
546+
547+
const internalNode = new LGraphNode('Internal Source')
548+
internalNode.addOutput('out', '*')
549+
subgraph.add(internalNode)
550+
551+
const subgraphOutput = subgraph.outputNode.slots[0]
552+
const link = subgraphOutput.connect(internalNode.outputs[0], internalNode)
553+
if (!link) throw new Error('Expected link')
554+
555+
// Simulate stale/corrupt bookkeeping where a dead link id remains.
556+
const staleLinkId = 999_999
557+
subgraphOutput.linkIds.push(staleLinkId)
558+
559+
subgraphOutput.disconnect()
560+
561+
expect(subgraphOutput.linkIds).toEqual([])
562+
expect(subgraph.links.get(link.id)).toBeUndefined()
563+
expect(internalNode.outputs[0].links).toEqual([])
564+
}
565+
)
512566
})
513567

514568
describe('SubgraphIO - Boundary Connection Management', () => {

src/lib/litegraph/src/subgraph/SubgraphInputNode.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,19 +171,32 @@ export class SubgraphInputNode
171171
): void {
172172
const { subgraph } = this
173173

174+
const slotIndex = node.inputs.findIndex((inp) => inp === input)
175+
if (slotIndex === -1) {
176+
console.warn('disconnectNodeInput: target input slot not found', this)
177+
return
178+
}
179+
174180
const subgraphInput = link ? this.slots.at(link.origin_slot) : undefined
175181
if (!subgraphInput) {
176182
console.warn(
177183
'disconnectNodeInput: subgraphInput not found',
178184
this,
179185
link?.origin_slot
180186
)
181-
return
182-
}
183187

184-
const slotIndex = node.inputs.findIndex((inp) => inp === input)
185-
if (slotIndex === -1) {
186-
console.warn('disconnectNodeInput: target input slot not found', this)
188+
if (input._floatingLinks?.size) {
189+
for (const floatingLink of input._floatingLinks) {
190+
subgraph.removeFloatingLink(floatingLink)
191+
}
192+
}
193+
194+
input.link = null
195+
if (link) {
196+
subgraph.disconnectLink(link, 'output')
197+
subgraph._version++
198+
}
199+
subgraph.setDirtyCanvas(false, true)
187200
return
188201
}
189202

src/lib/litegraph/src/subgraph/SubgraphOutput.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,7 @@ export class SubgraphOutput extends SubgraphSlot {
153153
slot: this
154154
})
155155
}
156+
157+
this.linkIds.length = 0
156158
}
157159
}

0 commit comments

Comments
 (0)