diff --git a/src/extensions/core/simpleTouchSupport.test.ts b/src/extensions/core/simpleTouchSupport.test.ts index 596f0458099..be4fb075967 100644 --- a/src/extensions/core/simpleTouchSupport.test.ts +++ b/src/extensions/core/simpleTouchSupport.test.ts @@ -14,23 +14,23 @@ type Viewport = Pick< 'scale' | 'offset' | 'min_scale' | 'max_scale' > -const { canvasEl, container, canvasAvailability } = vi.hoisted(() => { +const { canvasEl, container, canvasAvailability, ds } = vi.hoisted(() => { const container = document.createElement('div') const canvasEl = document.createElement('canvas') container.append(canvasEl) - return { container, canvasEl, canvasAvailability: { missing: false } } -}) - -const processMouseDown = vi.fn() -const processMouseMove = vi.fn() - -vi.mock(import('@/scripts/app'), () => { const ds: Viewport = { scale: 1, offset: [0, 0], min_scale: 0.1, max_scale: 10 } + return { container, canvasEl, canvasAvailability: { missing: false }, ds } +}) + +const processMouseDown = vi.fn() +const processMouseMove = vi.fn() + +vi.mock(import('@/scripts/app'), () => { const canvas = fromPartial({ ds, pointer: { isDown: false }, @@ -164,14 +164,81 @@ describe('Comfy.SimpleTouchSupport', () => { it('ignores a pinch when no canvas exists', () => { withoutCanvas() + const listenerErrors = vi.fn() + window.addEventListener('error', listenerErrors) + onTestFinished(() => window.removeEventListener('error', listenerErrors)) const twoFingers = [touchAt(canvasEl), touchAt(canvasEl, 100)] dispatchTouch('touchstart', twoFingers) dispatchTouch('touchmove', twoFingers) pressPointer({ isPrimary: true }) + expect(listenerErrors).not.toHaveBeenCalled() expect(processMouseDown).toHaveBeenCalledOnce() }) + + describe('pinch zoom', () => { + function resetViewport() { + ds.scale = 1 + ds.offset[0] = 0 + ds.offset[1] = 0 + } + + it.for([ + { name: 'start on the same point', start: 0, move: 100 }, + { name: 'start and stay on the same point', start: 0, move: 0 }, + { name: 'end on the same point', start: 100, move: 0 } + ])( + 'leaves the viewport unchanged when two fingers $name', + ({ start, move }) => { + resetViewport() + + dispatchTouch('touchstart', [ + touchAt(canvasEl), + touchAt(canvasEl, start) + ]) + dispatchTouch('touchmove', [touchAt(canvasEl), touchAt(canvasEl, move)]) + + expect({ scale: ds.scale, offset: [...ds.offset] }).toEqual({ + scale: 1, + offset: [0, 0] + }) + } + ) + + it('waits for a usable distance before measuring a pinch', () => { + resetViewport() + + dispatchTouch('touchstart', [touchAt(canvasEl), touchAt(canvasEl)]) + dispatchTouch('touchmove', [touchAt(canvasEl), touchAt(canvasEl, 7)]) + dispatchTouch('touchmove', [touchAt(canvasEl), touchAt(canvasEl, 50)]) + + expect(ds.scale).toBe(1) + }) + + it('zooms once fingers that started on the same point move apart', () => { + resetViewport() + + dispatchTouch('touchstart', [touchAt(canvasEl), touchAt(canvasEl)]) + dispatchTouch('touchmove', [touchAt(canvasEl), touchAt(canvasEl, 50)]) + dispatchTouch('touchmove', [touchAt(canvasEl), touchAt(canvasEl, 100)]) + + expect(ds.scale).toBe(2) + }) + + it('uses the minimum scale when converting pinch movement to an offset', () => { + resetViewport() + ds.scale = ds.min_scale + + dispatchTouch('touchstart', [touchAt(canvasEl), touchAt(canvasEl, 100)]) + dispatchTouch('touchmove', [touchAt(canvasEl, 20), touchAt(canvasEl, 70)]) + + expect({ scale: ds.scale, offset: [...ds.offset] }).toEqual({ + scale: 0.1, + offset: [-50, 0] + }) + }) + }) }) describe('touch pointer guards', () => { diff --git a/src/extensions/core/simpleTouchSupport.ts b/src/extensions/core/simpleTouchSupport.ts index ffd8c627f39..60060b390c6 100644 --- a/src/extensions/core/simpleTouchSupport.ts +++ b/src/extensions/core/simpleTouchSupport.ts @@ -9,6 +9,8 @@ import type { ComfyExtension } from '@/types/comfy' type TouchPoint = { clientX: number; clientY: number } +const MIN_PINCH_DISTANCE = 8 + function zoomCanvasAroundPinch( canvas: LGraphCanvas, requestedScale: number, @@ -16,8 +18,6 @@ function zoomCanvasAroundPinch( 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 @@ -25,6 +25,8 @@ function zoomCanvasAroundPinch( scale = canvas.ds.max_scale } + const newX = (center.clientX - lastTouch.clientX) / scale + const newY = (center.clientY - lastTouch.clientY) / scale const oldScale = canvas.ds.scale canvas.ds.scale = scale @@ -143,6 +145,32 @@ export const simpleTouchSupportExtension: ComfyExtension = { app.canvasEl.parentElement?.addEventListener('touchcancel', resetTouchState) + function handlePinchMove(e: TouchEvent) { + if (!lastTouch) return + const canvas = canvasOrReport('touchmove', e) + if (!canvas) return + e.preventDefault() + canvas.pointer.isDown = false + touchGesture.zooming = true + + LiteGraph.closeAllContextMenus(window) + canvas.search_box?.close() + const newTouchDist = getMultiTouchPos(e) + const center = getMultiTouchCenter(e) + + if (lastScale === null || touchDist === null) return + if (touchDist < MIN_PINCH_DISTANCE) { + touchDist = newTouchDist + lastScale = canvas.ds.scale + lastTouch = center + return + } + const scale = (lastScale * newTouchDist) / touchDist + if (!Number.isFinite(scale) || scale <= 0) return + + zoomCanvasAroundPinch(canvas, scale, center, lastTouch) + } + app.canvasEl.parentElement?.addEventListener( 'touchmove', (e) => { @@ -154,23 +182,8 @@ export const simpleTouchSupportExtension: ComfyExtension = { touchTime = null } } - if (e.touches.length === 2 && lastTouch && !e.ctrlKey && !e.shiftKey) { - const canvas = canvasOrReport('touchmove', e) - if (!canvas) return - e.preventDefault() - canvas.pointer.isDown = false - touchGesture.zooming = true - - LiteGraph.closeAllContextMenus(window) - canvas.search_box?.close() - const newTouchDist = getMultiTouchPos(e) - - const center = getMultiTouchCenter(e) - - if (lastScale === null || touchDist === null) return - const scale = (lastScale * newTouchDist) / touchDist - - zoomCanvasAroundPinch(canvas, scale, center, lastTouch) + if (e.touches.length === 2 && !e.ctrlKey && !e.shiftKey) { + handlePinchMove(e) } }, true diff --git a/src/lib/litegraph/src/LGraphCanvas.startRendering.test.ts b/src/lib/litegraph/src/LGraphCanvas.startRendering.test.ts index 7d2f28f4346..5e60fe5d9b6 100644 --- a/src/lib/litegraph/src/LGraphCanvas.startRendering.test.ts +++ b/src/lib/litegraph/src/LGraphCanvas.startRendering.test.ts @@ -2,24 +2,240 @@ 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' +import { + createMockCanvasRenderingContext2D, + createTestCanvasElement +} from '@/utils/__tests__/canvasTestUtils' + +function captureFrames() { + const frames: FrameRequestCallback[] = [] + const requestAnimationFrame = vi + .spyOn(window, 'requestAnimationFrame') + .mockImplementation((callback) => { + frames.push(callback) + return frames.length + }) + function runNextFrame() { + const frame = frames.shift() + if (!frame) throw new Error('No frame was scheduled') + frame(0) + } + function runFrames(count: number) { + for (let i = 0; i < count; i++) runNextFrame() + } + function drainFrames( + limit: number, + beforeFrame: (index: number) => void = () => {} + ) { + for (let i = 0; i < limit && frames.length > 0; i++) { + beforeFrame(i) + runNextFrame() + } + } + return { + requestAnimationFrame, + runNextFrame, + runFrames, + drainFrames, + frames + } +} + +function createCanvas() { + return new LGraphCanvas(createTestCanvasElement(), new LGraph(), { + skip_events: true + }) +} vi.mock(import('@/platform/telemetry/reportError')) describe('LGraphCanvas.startRendering', () => { - it('finishes constructing and reports when the first frame throws', () => { + it('finishes constructing, reports, and keeps rendering when the first frame throws', () => { + const { requestAnimationFrame } = captureFrames() const drawFailure = new TypeError('drawImage is patched') + vi.spyOn(LGraphCanvas.prototype, 'draw') + .mockImplementation(() => {}) + .mockImplementationOnce(() => { + throw drawFailure + }) + + const canvas = createCanvas() + + expect(canvas.is_rendering).toBe(true) + expect(requestAnimationFrame).toHaveBeenCalledOnce() + expect(reportError).toHaveBeenCalledExactlyOnceWith( + drawFailure, + expect.objectContaining({ + errorType: 'error_rendering_canvas_initial_frame' + }) + ) + }) + + it('restarts the failure budget after a frame draws successfully', () => { + const { runFrames, frames } = captureFrames() + let calls = 0 + const draw = vi + .spyOn(LGraphCanvas.prototype, 'draw') + .mockImplementation(() => { + calls++ + if (calls !== 60) throw new TypeError('transient') + }) + + const canvas = createCanvas() + runFrames(118) + + expect(draw).toHaveBeenCalledTimes(119) + expect(canvas.is_rendering).toBe(true) + expect(frames).toHaveLength(1) + expect(reportError).toHaveBeenCalledOnce() + }) + + it.for([ + { name: 'same type, different message', second: new TypeError('second') }, + { name: 'different type, same message', second: new RangeError('first') }, + { name: 'literal string matching an error key', second: 'TypeError:first' } + ])('reports each distinct drawing failure once: $name', ({ second }) => { + const { runFrames } = captureFrames() + const first = new TypeError('first') + vi.spyOn(LGraphCanvas.prototype, 'draw') + .mockImplementation(() => {}) + .mockImplementationOnce(() => { + throw first + }) + .mockImplementationOnce(() => { + throw first + }) + .mockImplementationOnce(() => { + throw second + }) + + createCanvas() + runFrames(2) + + expect( + vi + .mocked(reportError) + .mock.calls.map(([cause, options]) => [cause, options.errorType]) + ).toEqual([ + [first, 'error_rendering_canvas_initial_frame'], + [second, 'error_rendering_canvas_frame'] + ]) + }) + + it('stops after 60 consecutive failed frames, reports once more, and can restart', () => { + const { runFrames, frames } = captureFrames() vi.spyOn(LGraphCanvas.prototype, 'draw').mockImplementation(() => { - throw drawFailure + throw new TypeError('drawImage is patched') }) - const canvas = new LGraphCanvas(createTestCanvasElement(), new LGraph(), { - skip_events: true - }) + const canvas = createCanvas() + runFrames(58) + + expect(canvas.is_rendering).toBe(true) + expect(frames).toHaveLength(1) + + runFrames(1) expect(canvas.is_rendering).toBe(false) + expect(frames).toHaveLength(0) + expect( + vi.mocked(reportError).mock.calls.map(([, o]) => [o.errorType, o.context]) + ).toEqual([ + ['error_rendering_canvas_initial_frame', undefined], + ['error_rendering_canvas_stopped', { failedFrames: 60 }] + ]) + + vi.mocked(LGraphCanvas.prototype.draw).mockImplementation(() => {}) + canvas.startRendering() + expect(canvas.is_rendering).toBe(true) + }) + + it('reports a failure that first happens on a later frame and keeps rendering', () => { + const { runNextFrame, frames } = captureFrames() + const drawFailure: unknown = Object.assign(Object.create(null), { + message: 'extension draw failed' + }) + vi.spyOn(LGraphCanvas.prototype, 'draw') + .mockImplementation(() => {}) + .mockImplementationOnce(() => {}) + .mockImplementationOnce(() => { + throw drawFailure + }) + + const canvas = createCanvas() + runNextFrame() + + expect(canvas.is_rendering).toBe(true) + expect(frames).toHaveLength(1) expect(reportError).toHaveBeenCalledExactlyOnceWith( drawFailure, + expect.objectContaining({ errorType: 'error_rendering_canvas_frame' }) + ) + }) + + it('resets every drawing context and redraws everything after a failed frame', () => { + captureFrames() + const ctx = createMockCanvasRenderingContext2D() + const bgctx = createMockCanvasRenderingContext2D() + const overlayCtx = createMockCanvasRenderingContext2D() + vi.spyOn(LGraphCanvas.prototype, 'draw') + .mockImplementation(() => {}) + .mockImplementationOnce(function (this: LGraphCanvas) { + this.dirty_canvas = false + this.dirty_bgcanvas = false + throw new TypeError('mid-frame') + }) + + vi.stubGlobal('devicePixelRatio', 2) + const canvas = new LGraphCanvas( + createTestCanvasElement({ ctx }), + new LGraph(), + { skip_events: true, skip_render: true } + ) + canvas.bgcanvas = createTestCanvasElement({ ctx: bgctx }) + canvas.overlayCanvas = createTestCanvasElement({ ctx: overlayCtx }) + const resetFront = vi.spyOn(canvas.canvas, 'width', 'set') + const resetBackground = vi.spyOn(canvas.bgcanvas, 'width', 'set') + const resetOverlay = vi.spyOn(canvas.overlayCanvas, 'width', 'set') + + canvas.startRendering() + + expect(resetFront).toHaveBeenCalledOnce() + expect(resetBackground).toHaveBeenCalledOnce() + expect(resetOverlay).toHaveBeenCalledOnce() + expect(ctx.setTransform).toHaveBeenLastCalledWith(2, 0, 0, 2, 0, 0) + expect(bgctx.setTransform).toHaveBeenLastCalledWith(2, 0, 0, 2, 0, 0) + expect(overlayCtx.setTransform).toHaveBeenLastCalledWith(1, 0, 0, 1, 0, 0) + expect([canvas.dirty_canvas, canvas.dirty_bgcanvas]).toEqual([true, true]) + }) + + it('stops after repeated failures even when idle frames succeed between them', () => { + const { drainFrames } = captureFrames() + const draw = vi.spyOn(LGraphCanvas.prototype, 'draw') + draw.mockImplementation(function (this: LGraphCanvas) { + if (this.dirty_canvas) throw new TypeError('dirty frames fail') + }) + + const canvas = createCanvas() + drainFrames(200, (index) => { + canvas.dirty_canvas = canvas.dirty_bgcanvas = index % 2 === 0 + }) + + expect(canvas.is_rendering).toBe(false) + }) + + it('finishes constructing when scheduling the first frame throws', () => { + const schedulerFailure = new TypeError('patched scheduler') + vi.spyOn(LGraphCanvas.prototype, 'draw').mockImplementation(() => {}) + vi.spyOn(window, 'requestAnimationFrame').mockImplementation(() => { + throw schedulerFailure + }) + + const canvas = createCanvas() + + expect(canvas.is_rendering).toBe(false) + expect(reportError).toHaveBeenCalledExactlyOnceWith( + schedulerFailure, expect.objectContaining({ errorType: 'error_rendering_canvas_initial_frame' }) @@ -27,14 +243,10 @@ describe('LGraphCanvas.startRendering', () => { }) it('keeps scheduling frames when the first frame succeeds', () => { - const requestAnimationFrame = vi - .spyOn(window, 'requestAnimationFrame') - .mockReturnValue(0) + const { requestAnimationFrame } = captureFrames() vi.spyOn(LGraphCanvas.prototype, 'draw').mockImplementation(() => {}) - const canvas = new LGraphCanvas(createTestCanvasElement(), new LGraph(), { - skip_events: true - }) + const canvas = createCanvas() expect(canvas.is_rendering).toBe(true) expect(requestAnimationFrame).toHaveBeenCalledOnce() diff --git a/src/lib/litegraph/src/LGraphCanvas.ts b/src/lib/litegraph/src/LGraphCanvas.ts index 2069a5cbe0c..c4faa9d2cdd 100644 --- a/src/lib/litegraph/src/LGraphCanvas.ts +++ b/src/lib/litegraph/src/LGraphCanvas.ts @@ -545,6 +545,8 @@ interface SnappingLink { endDirection?: LinkDirection } +const MAX_FAILED_FRAMES = 60 + /** * This class is in charge of rendering one graph inside a canvas. And provides all the interaction required. * Valid callbacks are: onNodeSelected, onNodeDeselected, onShowNodePanel, onNodeDblClicked @@ -2517,20 +2519,63 @@ export class LGraphCanvas implements CustomEventDispatcher if (this.is_rendering) return this.is_rendering = true - try { - renderFrame.call(this) - } catch (cause) { - this.is_rendering = false + let isFirstFrame = true + let failedFrames = 0 + const reportedFailures = new Set() + + function reportFrameFailure(cause: unknown) { + const key = + cause instanceof Error + ? `error:${cause.name}:${cause.message}` + : typeof cause === 'string' + ? `string:${cause}` + : cause + if (reportedFailures.has(key)) return + reportedFailures.add(key) reportError(cause, { - errorType: 'error_rendering_canvas_initial_frame', + errorType: isFirstFrame + ? 'error_rendering_canvas_initial_frame' + : 'error_rendering_canvas_frame', surface: 'graph', tags: { feature_area: 'litegraph', operation: 'start_rendering' } }) } + const recoverFromFailedFrame = (cause: unknown) => { + failedFrames++ + reportFrameFailure(cause) + const { dpr, overlayCanvas } = this + for (const canvas of new Set([ + this.canvas, + this.bgcanvas, + overlayCanvas + ])) { + if (!canvas) continue + const { width } = canvas + canvas.width = width + const scale = canvas === overlayCanvas ? 1 : dpr + canvas.getContext('2d')?.setTransform(scale, 0, 0, scale, 0, 0) + } + this.setDirty(true, true) + if (failedFrames < MAX_FAILED_FRAMES) return + this.is_rendering = false + reportError(cause, { + errorType: 'error_rendering_canvas_stopped', + surface: 'graph', + tags: { feature_area: 'litegraph', operation: 'start_rendering' }, + context: { failedFrames } + }) + } + function renderFrame(this: LGraphCanvas) { if (!this.pause_rendering) { - this.draw() + const drawsSomething = this.dirty_canvas || this.dirty_bgcanvas + try { + this.draw() + if (drawsSomething) failedFrames = 0 + } catch (cause) { + recoverFromFailedFrame(cause) + } } const window = this.getCanvasWindow() @@ -2543,6 +2588,14 @@ export class LGraphCanvas implements CustomEventDispatcher window.requestAnimationFrame(renderFrame.bind(this)) } } + isFirstFrame = false + } + + try { + renderFrame.call(this) + } catch (cause) { + this.is_rendering = false + reportFrameFailure(cause) } } diff --git a/src/platform/telemetry/perf/bootstrapTracer.test.ts b/src/platform/telemetry/perf/bootstrapTracer.test.ts index 384bcf78ea8..cc1e966041a 100644 --- a/src/platform/telemetry/perf/bootstrapTracer.test.ts +++ b/src/platform/telemetry/perf/bootstrapTracer.test.ts @@ -186,6 +186,15 @@ describe('bootstrapTracer', () => { expect([before, tracer.outcome]).toEqual(['pending', 'failed']) }) + it('exposes a timed-out startup as timed out, not pending', async () => { + const tracer = new BootstrapTracer() + + tracer.armWatchdog(30_000) + await vi.advanceTimersByTimeAsync(30_000) + + expect(tracer.outcome).toBe('timed_out') + }) + it('reports only once', () => { const telemetry = sharedUseTelemetry() if (!telemetry) throw new Error('Expected telemetry mock') @@ -279,6 +288,7 @@ describe('bootstrapTracer', () => { .mocked(telemetry.trackBootstrapComplete) .mock.calls.map(([metadata]) => metadata.outcome) ).toEqual(['timed_out', 'completed']) + expect(tracer.outcome).toBe('completed') }) it('suspends cancellation reporting and lets a replacement complete', async () => { diff --git a/src/platform/telemetry/perf/bootstrapTracer.ts b/src/platform/telemetry/perf/bootstrapTracer.ts index 2f250dc83d2..269a3fa2e9d 100644 --- a/src/platform/telemetry/perf/bootstrapTracer.ts +++ b/src/platform/telemetry/perf/bootstrapTracer.ts @@ -224,8 +224,12 @@ export class BootstrapTracer { return [...this._timings].sort((a, b) => a.startMs - b.startMs) } - get outcome(): 'completed' | 'failed' | 'pending' { - return this._outcome ?? 'pending' + /** + * `timed_out` is not terminal: startup can still finish after the watchdog + * fires, and the outcome then becomes `completed` or `failed`. + */ + get outcome(): 'completed' | 'failed' | 'timed_out' | 'pending' { + return this._outcome ?? (this._timeoutReported ? 'timed_out' : 'pending') } /**