Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix: unwind local image generation state on preparation rejection

Adam Eivy9c8465747f54
diff --git a/server/services/imageGen/local.js b/server/services/imageGen/local.js
index 093061913a..093a7e4c64 100644
--- a/server/services/imageGen/local.js
+++ b/server/services/imageGen/local.js
@@ -712,29 +712,32 @@ export async function generateImage({ pythonPath, prompt = '', negativePrompt =
// per inference step here; we watch and stream the latest as `currentImage`.
const stepwiseDir = await mkdtemp(join(tmpdir(), 'portos-stepwise-'));
- // `renderPrompt` — the user's prompt plus any LoRA activation tokens it was
- // missing (#4665). Identical to `prompt` when nothing was woven.
- const { bin, args } = buildArgs({ pythonPath, model, prompt: renderPrompt, negativePrompt, width: Number(width), height: Number(height), steps: actualSteps, guidance: actualGuidance, seed: actualSeed, quantize, outputPath, loraPaths: validLoras, loraScales, stepwiseDir, initImagePath: validInitImagePath, initImageStrength: validInitImageStrength, referenceImagePaths: validReferenceImagePaths, referenceImageStrengths: validReferenceImageStrengths });
- const heavyClaim = await claimHeavyLocalJob({ kind: 'local image generation', id: jobId });
- if (!heavyClaim.ok) {
- jobs.delete(jobId);
- await rmGuarded(stepwiseDir, { recursive: true, force: true });
- throw new ServerError(heavyClaim.message, { status: 409, code: 'HEAVY_LOCAL_JOB_BUSY', context: { holder: heavyClaim.holder } });
- }
- const releaseHeavyClaim = () => maintenance.continueSettlement(() => heavyClaim.release())
- .catch((err) => {
- maintenance.markCurrentUnsettled();
- console.error(`❌ Image generation claim release [${jobId.slice(0, 8)}]: ${err.message}`);
- });
+ let heavyClaim = null;
+ const releaseHeavyClaim = () => {
+ if (!heavyClaim?.release) return Promise.resolve();
+ return maintenance.continueSettlement(() => heavyClaim.release())
+ .catch((err) => {
+ maintenance.markCurrentUnsettled();
+ console.error(`❌ Image generation claim release [${jobId.slice(0, 8)}]: ${err.message}`);
+ });
+ };
const removeStepwise = () => maintenance.continueSettlement(
() => rmGuarded(stepwiseDir, { recursive: true, force: true }),
).catch((err) => {
maintenance.markCurrentUnsettled();
console.error(`❌ Image generation scratch cleanup [${jobId.slice(0, 8)}]: ${err.message}`);
});
+ let bin, args;
let proc;
let claimHandedOff = false;
try {
+ // `renderPrompt` — the user's prompt plus any LoRA activation tokens it was
+ // missing (#4665). Identical to `prompt` when nothing was woven.
+ ({ bin, args } = buildArgs({ pythonPath, model, prompt: renderPrompt, negativePrompt, width: Number(width), height: Number(height), steps: actualSteps, guidance: actualGuidance, seed: actualSeed, quantize, outputPath, loraPaths: validLoras, loraScales, stepwiseDir, initImagePath: validInitImagePath, initImageStrength: validInitImageStrength, referenceImagePaths: validReferenceImagePaths, referenceImageStrengths: validReferenceImageStrengths }));
+ heavyClaim = await claimHeavyLocalJob({ kind: 'local image generation', id: jobId });
+ if (!heavyClaim.ok) {
+ throw new ServerError(heavyClaim.message, { status: 409, code: 'HEAVY_LOCAL_JOB_BUSY', context: { holder: heavyClaim.holder } });
+ }
const { prepareLocalMemory, gpuBlockersMessage } = await import('../localMemory.js');
const memoryReport = await prepareLocalMemory();
// Something the unload above cannot evict already owns the GPU (today: the
diff --git a/server/services/imageGen/local.test.js b/server/services/imageGen/local.test.js
index c359eb1d64..d4a77ae6b1 100644
--- a/server/services/imageGen/local.test.js
+++ b/server/services/imageGen/local.test.js
@@ -1047,6 +1047,48 @@ describe('imageGen local.generateImage runtime pre-flight', () => {
});
expect(mockIsFlux2VenvHealthy).toHaveBeenCalledWith('QwenImage21Pipeline');
});
+
+ it('cleans up stepwise directory and purges the job from memory when buildArgs rejects', async () => {
+ mockResolveFlux2Python.mockReturnValue(null);
+ mockIsFlux2VenvHealthy.mockResolvedValue(true);
+ const fileUtils = await import('../../lib/fileUtils.js');
+ const rmSpy = vi.spyOn(fileUtils, 'rmGuarded');
+ const { attachSseClient } = await import('./local.js');
+ const testJobId = 'test-build-args-throw-' + Date.now();
+ await expect(generateImage({ modelId: 'qwen-image-2.1', prompt: 'a fox', jobId: testJobId })).rejects.toMatchObject({
+ code: 'IMAGE_GEN_FLUX2_NOT_INSTALLED',
+ status: 400,
+ });
+ const response = { writeHead: vi.fn(), write: vi.fn(), end: vi.fn(), req: new EventEmitter() };
+ expect(attachSseClient(testJobId, response)).toBe(false);
+ expect(rmSpy).toHaveBeenCalledWith(expect.stringContaining('portos-stepwise-'), { recursive: true, force: true });
+ rmSpy.mockRestore();
+ });
+
+ it('cleans up stepwise directory and purges the job from memory when heavy claim is busy', async () => {
+ mockResolveFlux2Python.mockReturnValue('/fake/venv-flux2/bin/python3');
+ mockIsFlux2VenvHealthy.mockResolvedValue(true);
+ const heavyJobClaim = await import('../../lib/heavyJobClaim.js');
+ const claimSpy = vi.spyOn(heavyJobClaim, 'claimHeavyLocalJob').mockResolvedValueOnce({
+ ok: false,
+ message: 'busy with another render',
+ holder: { id: 'other-job' },
+ release: async () => {},
+ });
+ const fileUtils = await import('../../lib/fileUtils.js');
+ const rmSpy = vi.spyOn(fileUtils, 'rmGuarded');
+ const { attachSseClient } = await import('./local.js');
+ const testJobId = 'test-heavy-busy-' + Date.now();
+ await expect(generateImage({ modelId: 'qwen-image-2.1', prompt: 'a fox', jobId: testJobId })).rejects.toMatchObject({
+ code: 'HEAVY_LOCAL_JOB_BUSY',
+ status: 409,
+ });
+ const response = { writeHead: vi.fn(), write: vi.fn(), end: vi.fn(), req: new EventEmitter() };
+ expect(attachSseClient(testJobId, response)).toBe(false);
+ expect(rmSpy).toHaveBeenCalledWith(expect.stringContaining('portos-stepwise-'), { recursive: true, force: true });
+ claimSpy.mockRestore();
+ rmSpy.mockRestore();
+ });
});
// Regression: SSE had the Python diagnosis, but the queue and status snapshot