From 093bc8e9c3aebeac6006380e93a4154f0f9c153b Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 18 Oct 2026 23:00:52 +0000 Subject: [PATCH] fix: guard touch handlers against a missing canvas and report why The processMouseDown wrapper read app.canvas, which stays undefined when ComfyApp.setup() aborts inside the LGraphCanvas constructor, so every click threw. Read the canvas from the receiver instead, bail out of the touch handlers when no canvas exists, and report the first occurrence per entry point with diagnostic context. Also stop a throwing first frame from aborting LGraphCanvas construction: the failure is reported and the instance finishes constructing. --- .../core/simpleTouchSupport.test.ts | 52 ++++++-- src/extensions/core/simpleTouchSupport.ts | 115 ++++++++++-------- src/extensions/core/touchCanvasAccess.test.ts | 71 +++++++++++ src/extensions/core/touchCanvasAccess.ts | 37 ++++++ .../src/LGraphCanvas.startRendering.test.ts | 43 +++++++ src/lib/litegraph/src/LGraphCanvas.ts | 15 ++- .../telemetry/perf/bootstrapTracer.test.ts | 10 ++ .../telemetry/perf/bootstrapTracer.ts | 6 + 8 files changed, 282 insertions(+), 67 deletions(-) create mode 100644 src/extensions/core/touchCanvasAccess.test.ts create mode 100644 src/extensions/core/touchCanvasAccess.ts create mode 100644 src/lib/litegraph/src/LGraphCanvas.startRendering.test.ts diff --git a/src/extensions/core/simpleTouchSupport.test.ts b/src/extensions/core/simpleTouchSupport.test.ts index e2205461c37..aca6ae976c9 100644 --- a/src/extensions/core/simpleTouchSupport.test.ts +++ b/src/extensions/core/simpleTouchSupport.test.ts @@ -1,5 +1,6 @@ import { fromPartial } from '@total-typescript/shoehorn' -import { beforeEach, describe, expect, it, vi } from 'vitest' +import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest' +import { shallowRef } from 'vue' import type { DragAndScale } from '@/lib/litegraph/src/litegraph' import { LGraphCanvas } from '@/lib/litegraph/src/litegraph' @@ -11,11 +12,11 @@ type Viewport = Pick< 'scale' | 'offset' | 'min_scale' | 'max_scale' > -const { canvasEl, container } = vi.hoisted(() => { +const { canvasEl, container, canvasAvailability } = vi.hoisted(() => { const container = document.createElement('div') const canvasEl = document.createElement('canvas') container.append(canvasEl) - return { container, canvasEl } + return { container, canvasEl, canvasAvailability: { missing: false } } }) const processMouseDown = vi.fn() @@ -27,15 +28,19 @@ vi.mock(import('@/scripts/app'), () => { min_scale: 0.1, max_scale: 10 } + const canvas = fromPartial({ + ds, + pointer: { isDown: false }, + setDirty: vi.fn() + }) return { app: fromPartial({ registerExtension: (ext: ComfyExtension) => ext.setup?.(fromPartial({})), canvasEl, - canvas: fromPartial({ - ds, - pointer: { isDown: false }, - setDirty: vi.fn() - }) + canvasElRef: shallowRef(canvasEl), + get canvasOrUndefined() { + return canvasAvailability.missing ? undefined : canvas + } }) } }) @@ -71,10 +76,19 @@ function dispatchTouch( } function pressPointer(init: PointerEventInit) { + const canvas = fromPartial({ pointer: { isDown: true } }) LGraphCanvas.prototype.processMouseDown.call( - fromPartial({}), + canvas, new PointerEvent('pointerdown', { pointerType: 'touch', ...init }) ) + return canvas +} + +function withoutCanvas() { + canvasAvailability.missing = true + onTestFinished(() => { + canvasAvailability.missing = false + }) } /** @@ -127,4 +141,24 @@ describe('Comfy.SimpleTouchSupport', () => { expect(processMouseDown).not.toHaveBeenCalled() } ) + + it('presses through to the original when no canvas exists', () => { + withoutCanvas() + + const canvas = pressPointer({ isPrimary: true }) + + expect(processMouseDown).toHaveBeenCalledOnce() + expect(canvas.pointer.isDown).toBe(false) + }) + + it('ignores a pinch when no canvas exists', () => { + withoutCanvas() + const twoFingers = [touchAt(canvasEl), touchAt(canvasEl, 100)] + + dispatchTouch('touchstart', twoFingers) + dispatchTouch('touchmove', twoFingers) + pressPointer({ isPrimary: true }) + + expect(processMouseDown).toHaveBeenCalledOnce() + }) }) diff --git a/src/extensions/core/simpleTouchSupport.ts b/src/extensions/core/simpleTouchSupport.ts index d3ee7d47fab..ed084b30903 100644 --- a/src/extensions/core/simpleTouchSupport.ts +++ b/src/extensions/core/simpleTouchSupport.ts @@ -2,9 +2,55 @@ import { LGraphCanvas, LiteGraph } from '@/lib/litegraph/src/litegraph' import { app } from '@/scripts/app' +import { canvasOrReport } from './touchCanvasAccess' + let touchZooming = false let touchCount = 0 +type TouchPoint = { clientX: number; clientY: number } + +function zoomCanvasAroundPinch( + canvas: LGraphCanvas, + requestedScale: number, + center: TouchPoint, + lastTouch: TouchPoint +) { + let scale = requestedScale + const newX = (center.clientX - lastTouch.clientX) / requestedScale + const newY = (center.clientY - lastTouch.clientY) / requestedScale + + if (scale < canvas.ds.min_scale) { + scale = canvas.ds.min_scale + } else if (scale > canvas.ds.max_scale) { + scale = canvas.ds.max_scale + } + + const oldScale = canvas.ds.scale + + canvas.ds.scale = scale + + if (Math.abs(canvas.ds.scale - 1) < 0.01) { + canvas.ds.scale = 1 + } + + const newScale = canvas.ds.scale + + const convertScaleToOffset = (scale: number) => [ + center.clientX / scale - canvas.ds.offset[0], + center.clientY / scale - canvas.ds.offset[1] + ] + const oldCenter = convertScaleToOffset(oldScale) + const newCenter = convertScaleToOffset(newScale) + + canvas.ds.offset[0] += newX + newCenter[0] - oldCenter[0] + canvas.ds.offset[1] += newY + newCenter[1] - oldCenter[1] + + lastTouch.clientX = center.clientX + lastTouch.clientY = center.clientY + + canvas.setDirty(true, true) +} + app.registerExtension({ name: 'Comfy.SimpleTouchSupport', setup() { @@ -34,18 +80,18 @@ app.registerExtension({ lastTouch = null lastScale = null if (e.touches.length === 1) { - // Store start time for press+hold for context menu touchTime = new Date() lastTouch = e.touches[0] } else { touchTime = null if (e.touches.length === 2) { - // Store center pos for zoom - lastScale = app.canvas.ds.scale + const canvas = canvasOrReport('touchstart', e) + if (!canvas) return + lastScale = canvas.ds.scale lastTouch = getMultiTouchCenter(e) touchDist = getMultiTouchPos(e) - app.canvas.pointer.isDown = false + canvas.pointer.isDown = false } } }, @@ -62,15 +108,13 @@ app.registerExtension({ if (new Date().getTime() - touchTime.getTime() > 600) { if (e.target === app.canvasEl) { const touch = { - button: 2, // Right click + button: 2, clientX: e.changedTouches[0].clientX, clientY: e.changedTouches[0].clientY, - pointerId: 1, // changedTouches' id is 0, set it to any number - isPrimary: true // changedTouches' isPrimary is false, so set it to true + pointerId: 1, + isPrimary: true } - // context menu info set in 'pointerdown' event app.canvasEl.dispatchEvent(new PointerEvent('pointerdown', touch)) - // then, context menu open after 'pointerup' event setTimeout(() => { app.canvasEl.dispatchEvent(new PointerEvent('pointerup', touch)) }) @@ -91,21 +135,17 @@ app.registerExtension({ touchDist = null } - // Reset touch state when page loses visibility (e.g., switching apps on iPad) - // This prevents touchCount from getting stuck when touchend events are missed document.addEventListener('visibilitychange', () => { if (document.hidden) { resetTouchState() } }) - // Also handle touchcancel which fires when touch is interrupted app.canvasEl.parentElement?.addEventListener('touchcancel', resetTouchState) app.canvasEl.parentElement?.addEventListener( 'touchmove', (e) => { - // make a threshold for touchmove to prevent clear touchTime for long press if (touchTime && lastTouch && e.touches.length === 1) { const onlyTouch = e.touches[0] const deltaX = onlyTouch.clientX - lastTouch.clientX @@ -115,54 +155,22 @@ app.registerExtension({ } } if (e.touches.length === 2 && lastTouch && !e.ctrlKey && !e.shiftKey) { - e.preventDefault() // Prevent browser from zooming when two textareas are touched - app.canvas.pointer.isDown = false + const canvas = canvasOrReport('touchmove', e) + if (!canvas) return + e.preventDefault() + canvas.pointer.isDown = false touchZooming = true LiteGraph.closeAllContextMenus(window) - app.canvas.search_box?.close() + canvas.search_box?.close() const newTouchDist = getMultiTouchPos(e) const center = getMultiTouchCenter(e) if (lastScale === null || touchDist === null) return - let scale = (lastScale * newTouchDist) / touchDist - - const newX = (center.clientX - lastTouch.clientX) / scale - const newY = (center.clientY - lastTouch.clientY) / scale - - // Code from LiteGraph - if (scale < app.canvas.ds.min_scale) { - scale = app.canvas.ds.min_scale - } else if (scale > app.canvas.ds.max_scale) { - scale = app.canvas.ds.max_scale - } - - const oldScale = app.canvas.ds.scale - - app.canvas.ds.scale = scale - - // Code from LiteGraph - if (Math.abs(app.canvas.ds.scale - 1) < 0.01) { - app.canvas.ds.scale = 1 - } - - const newScale = app.canvas.ds.scale - - const convertScaleToOffset = (scale: number) => [ - center.clientX / scale - app.canvas.ds.offset[0], - center.clientY / scale - app.canvas.ds.offset[1] - ] - const oldCenter = convertScaleToOffset(oldScale) - const newCenter = convertScaleToOffset(newScale) - - app.canvas.ds.offset[0] += newX + newCenter[0] - oldCenter[0] - app.canvas.ds.offset[1] += newY + newCenter[1] - oldCenter[1] - - lastTouch.clientX = center.clientX - lastTouch.clientY = center.clientY + const scale = (lastScale * newTouchDist) / touchDist - app.canvas.setDirty(true, true) + zoomCanvasAroundPinch(canvas, scale, center, lastTouch) } }, true @@ -181,7 +189,8 @@ LGraphCanvas.prototype.processMouseDown = function (e: PointerEvent) { if (touchZooming || touchCount) { return } - app.canvas.pointer.isDown = false // Prevent context menu from opening on second tap + canvasOrReport('processMouseDown', e) + this.pointer.isDown = false return processMouseDown.apply(this, [e]) } diff --git a/src/extensions/core/touchCanvasAccess.test.ts b/src/extensions/core/touchCanvasAccess.test.ts new file mode 100644 index 00000000000..64dd18a247b --- /dev/null +++ b/src/extensions/core/touchCanvasAccess.test.ts @@ -0,0 +1,71 @@ +import { fromPartial } from '@total-typescript/shoehorn' +import { describe, expect, it, vi } from 'vitest' +import { shallowRef } from 'vue' + +import type { LGraphCanvas } from '@/lib/litegraph/src/litegraph' +import { bootstrapTracer } from '@/platform/telemetry/perf/bootstrapTracer' +import { reportError } from '@/platform/telemetry/reportError' +import { app } from '@/scripts/app' + +import { canvasOrReport } from './touchCanvasAccess' + +vi.mock(import('@/platform/telemetry/reportError')) + +const canvasEl = vi.hoisted(() => document.createElement('canvas')) + +vi.mock(import('@/scripts/app'), () => ({ + app: fromPartial({ + canvasElRef: shallowRef(canvasEl), + get canvasOrUndefined(): LGraphCanvas | undefined { + return undefined + } + }) +})) + +describe('canvasOrReport', () => { + it('returns the canvas without reporting when one exists', () => { + const canvas = fromPartial({}) + vi.spyOn(app, 'canvasOrUndefined', 'get').mockReturnValue(canvas) + + expect(canvasOrReport('has-canvas', new Event('touchstart'))).toBe(canvas) + expect(reportError).not.toHaveBeenCalled() + }) + + it('reports why the canvas is missing, once per entry point', () => { + document.body.append(canvasEl) + bootstrapTracer.complete('failed') + const press = new PointerEvent('pointerdown', { + pointerType: 'touch', + isPrimary: true + }) + + expect(canvasOrReport('press', press)).toBeUndefined() + canvasOrReport('press', press) + + expect(reportError).toHaveBeenCalledExactlyOnceWith( + expect.any(Error), + expect.objectContaining({ + errorType: 'error_handling_touch_input_missing_canvas', + context: { + entryPoint: 'press', + eventType: 'pointerdown', + pointerType: 'touch', + isPrimary: true, + documentReadyState: document.readyState, + bootstrapOutcome: 'failed', + hasWindowApp: false, + hasCanvasEl: true, + canvasElConnected: true, + targetIsCanvasEl: false + } + }) + ) + }) + + it('reports each entry point separately', () => { + canvasOrReport('pinch-start', new Event('touchstart')) + canvasOrReport('pinch-move', new Event('touchmove')) + + expect(reportError).toHaveBeenCalledTimes(2) + }) +}) diff --git a/src/extensions/core/touchCanvasAccess.ts b/src/extensions/core/touchCanvasAccess.ts new file mode 100644 index 00000000000..20894d14e50 --- /dev/null +++ b/src/extensions/core/touchCanvasAccess.ts @@ -0,0 +1,37 @@ +import { bootstrapTracer } from '@/platform/telemetry/perf/bootstrapTracer' +import { reportError } from '@/platform/telemetry/reportError' +import { app } from '@/scripts/app' + +const reportedEntryPoints = new Set() + +function reportMissingCanvas(entryPoint: string, e: Event) { + if (reportedEntryPoints.has(entryPoint)) return + reportedEntryPoints.add(entryPoint) + + const canvasEl = app.canvasElRef.value + const pointer = e instanceof PointerEvent ? e : undefined + reportError(new Error('Canvas unavailable while handling touch input'), { + errorType: 'error_handling_touch_input_missing_canvas', + surface: 'graph', + level: 'warning', + tags: { feature_area: 'extensions', operation: entryPoint }, + context: { + entryPoint, + eventType: e.type, + pointerType: pointer?.pointerType, + isPrimary: pointer?.isPrimary, + documentReadyState: document.readyState, + bootstrapOutcome: bootstrapTracer.outcome, + hasWindowApp: window.app !== undefined, + hasCanvasEl: canvasEl !== undefined, + canvasElConnected: canvasEl?.isConnected, + targetIsCanvasEl: canvasEl !== undefined && e.target === canvasEl + } + }) +} + +export function canvasOrReport(entryPoint: string, e: Event) { + const canvas = app.canvasOrUndefined + if (!canvas) reportMissingCanvas(entryPoint, e) + return canvas +} diff --git a/src/lib/litegraph/src/LGraphCanvas.startRendering.test.ts b/src/lib/litegraph/src/LGraphCanvas.startRendering.test.ts new file mode 100644 index 00000000000..7d2f28f4346 --- /dev/null +++ b/src/lib/litegraph/src/LGraphCanvas.startRendering.test.ts @@ -0,0 +1,43 @@ +import { describe, expect, it, vi } from 'vitest' + +import { LGraph, LGraphCanvas } from '@/lib/litegraph/src/litegraph' +import { reportError } from '@/platform/telemetry/reportError' +import { createTestCanvasElement } from '@/utils/__tests__/canvasTestUtils' + +vi.mock(import('@/platform/telemetry/reportError')) + +describe('LGraphCanvas.startRendering', () => { + it('finishes constructing and reports when the first frame throws', () => { + const drawFailure = new TypeError('drawImage is patched') + vi.spyOn(LGraphCanvas.prototype, 'draw').mockImplementation(() => { + throw drawFailure + }) + + const canvas = new LGraphCanvas(createTestCanvasElement(), new LGraph(), { + skip_events: true + }) + + expect(canvas.is_rendering).toBe(false) + expect(reportError).toHaveBeenCalledExactlyOnceWith( + drawFailure, + expect.objectContaining({ + errorType: 'error_rendering_canvas_initial_frame' + }) + ) + }) + + it('keeps scheduling frames when the first frame succeeds', () => { + const requestAnimationFrame = vi + .spyOn(window, 'requestAnimationFrame') + .mockReturnValue(0) + vi.spyOn(LGraphCanvas.prototype, 'draw').mockImplementation(() => {}) + + const canvas = new LGraphCanvas(createTestCanvasElement(), new LGraph(), { + skip_events: true + }) + + expect(canvas.is_rendering).toBe(true) + expect(requestAnimationFrame).toHaveBeenCalledOnce() + expect(reportError).not.toHaveBeenCalled() + }) +}) diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 2e4d9fb543e..c53b667b9fa 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -2450,13 +2450,20 @@ export class LGraphCanvas implements CustomEventDispatcher * */ startRendering(): void { - // already rendering if (this.is_rendering) return this.is_rendering = true - renderFrame.call(this) + try { + renderFrame.call(this) + } catch (cause) { + this.is_rendering = false + reportError(cause, { + errorType: 'error_rendering_canvas_initial_frame', + surface: 'graph', + tags: { feature_area: 'litegraph', operation: 'start_rendering' } + }) + } - /** Render loop */ function renderFrame(this: LGraphCanvas) { if (!this.pause_rendering) { this.draw() @@ -2465,12 +2472,10 @@ export class LGraphCanvas implements CustomEventDispatcher const window = this.getCanvasWindow() if (this.is_rendering) { if (this._maximumFrameGap > 0) { - // Manual FPS limit const gap = this._maximumFrameGap - (LiteGraph.getTime() - this.last_draw_time) setTimeout(renderFrame.bind(this), Math.max(1, gap)) } else { - // FPS limited by refresh rate window.requestAnimationFrame(renderFrame.bind(this)) } } diff --git a/src/platform/telemetry/perf/bootstrapTracer.test.ts b/src/platform/telemetry/perf/bootstrapTracer.test.ts index 4dbcfbfc921..5168683821c 100644 --- a/src/platform/telemetry/perf/bootstrapTracer.test.ts +++ b/src/platform/telemetry/perf/bootstrapTracer.test.ts @@ -142,6 +142,16 @@ describe('bootstrapTracer', () => { expect(setViewLoadingTime).not.toHaveBeenCalled() }) + it('exposes the startup outcome once it is known', () => { + const tracer = new BootstrapTracer() + const before = tracer.outcome + + tracer.complete('failed') + tracer.complete() + + expect([before, tracer.outcome]).toEqual(['pending', 'failed']) + }) + it('reports only once', () => { const telemetry = sharedUseTelemetry() if (!telemetry) throw new Error('Expected telemetry mock') diff --git a/src/platform/telemetry/perf/bootstrapTracer.ts b/src/platform/telemetry/perf/bootstrapTracer.ts index 6866adba592..65db7a2ec39 100644 --- a/src/platform/telemetry/perf/bootstrapTracer.ts +++ b/src/platform/telemetry/perf/bootstrapTracer.ts @@ -104,6 +104,7 @@ export class BootstrapTracer { private _spans = new Map() private _timings: BootstrapPhaseTiming[] = [] private _completed = false + private _outcome: 'completed' | 'failed' | undefined private _timeoutReported = false private _watchdog: ReturnType | undefined @@ -176,6 +177,10 @@ export class BootstrapTracer { return [...this._timings].sort((a, b) => a.startMs - b.startMs) } + get outcome(): 'completed' | 'failed' | 'pending' { + return this._outcome ?? 'pending' + } + /** * Close out startup: stop any phase still open, emit one aggregate event * carrying total wall-clock and the per-phase breakdown, and log the trace @@ -184,6 +189,7 @@ export class BootstrapTracer { complete(outcome: 'completed' | 'failed' = 'completed'): void { if (this._completed) return this._completed = true + this._outcome = outcome clearTimeout(this._watchdog) for (const phase of Array.from(this._spans.keys())) this._stopPhase(phase) this._report(outcome)