{"record":{"id":"ced10478beb7a123","repo":"janhq/jan","slug":"could-not-unload-the-model-stop-its-active-requests-and-try","errorCode":null,"errorMessage":"Could not unload the model. Stop its active requests and try again.","messagePattern":"Could not unload the model\\. Stop its active requests and try again\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"web-app/src/lib/coworkModelControls.ts","lineNumber":80,"sourceCode":"    const setting = coworkLocalModel(provider, modelId)?.settings?.ctx_len\n    const value = Number(setting?.controller_props?.value)\n    if (setting && Number.isInteger(value) && value > 0) {\n      const max = await models.getModelContextLimit(modelId, providerName)\n      if (max !== undefined && Number.isInteger(max) && max > 0)\n        context = { value, max, next: nextCoworkContextSize(value, max) }\n    }\n  }\n  if (context?.next === undefined)\n    throw new Error(\n      'No supported context increase is available. Check the model context limit in provider settings.'\n    )\n\n  if (providerName === 'llamacpp') {\n    await models.updateModelSettings(modelId, { ctx_len: context.next })\n  } else if ((await models.getActiveModels(providerName)).includes(modelId)) {\n    const result = await models.stopModel(modelId, providerName)\n    if (!result?.success)\n      throw new Error(\n        result?.error ??\n          'Could not unload the model. Stop its active requests and try again.'\n      )\n  }\n\n  // Publish only after application succeeds. Merge into live state without\n  // restoring an old provider snapshot or changing the user's selection.\n  const state = useModelProvider.getState()\n  const latest = state.getProviderByName(providerName)\n  if (!latest) return\n  state.updateProvider(providerName, {\n    models: latest.models.map((model) =>\n      model.id === modelId\n        ? ({\n            ...model,\n            settings: {\n              ...model.settings,\n              ctx_len: {","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/janhq/jan/blob/7205d770c1e097c3daf35a911176410e93bc5564/web-app/src/lib/coworkModelControls.ts#L62-L98","documentation":"Thrown by increaseCoworkContext when, for non-llamacpp providers with the model active, models.stopModel fails (result?.success falsy). Raising the context size requires unloading the model first; if stop fails the change is aborted, surfacing the backend's error message or this fallback.","triggerScenarios":"Calling stopModel(modelId, providerName) returns { success: false } or null — the model has in-flight requests it cannot kill, the backend is unreachable, or the stop endpoint returned an error that is empty (triggering the fallback message).","commonSituations":"An active chat/agent request is still streaming against the model; the provider backend hung; a race where another tab restarted the model; stale model state making the stop call target a non-running instance.","solutions":["Stop any active chats/requests using the model, wait a moment, then retry the context increase","Manually stop the model from the provider/model list UI and start it again","Restart the app or provider backend if the stop call keeps failing","Check backend logs for the real stopModel error (result.error) to address the root cause"],"exampleFix":"// before\nconst result = await models.stopModel(modelId, providerName)\nif (!result?.success) throw new Error(result?.error ?? 'Could not unload the model. Stop its active requests and try again.')\n// after\nconst result = await models.stopModel(modelId, providerName)\nif (!result?.success) {\n  await new Promise((r) => setTimeout(r, 1000))\n  const retried = await models.stopModel(modelId, providerName)\n  if (!retried?.success) throw new Error(retried?.error ?? 'Could not unload the model. Stop its active requests and try again.')\n}","handlingStrategy":"retry","validationCode":"const active = await models.getActiveModels(providerName)\nif (!active.includes(modelId)) return // model not running; no stop needed","typeGuard":"const stopSucceeded = (r: { success?: boolean } | null | undefined): r is { success: true } =>\n  !!r && r.success === true","tryCatchPattern":"try {\n  await increaseCoworkContext(modelId, providerName)\n} catch (e) {\n  if (e.message.includes('Could not unload the model')) {\n    showToast('Finish active requests on this model, stop it manually, then retry.')\n  }\n}","preventionTips":["Cancel active chats/streams before changing model settings","Retry stopModel after a short delay before failing","Surface result.error from the backend instead of only the fallback text","Track request liveness so the UI blocks context changes while streaming"],"tags":["model-lifecycle","unload","retry"],"backgroundTag":"invalid-state-transition","analyzedSha":"7205d770c1e097c3daf35a911176410e93bc5564","analyzedAt":"2026-09-17T14:27:30.100Z","contentChangedAt":"2026-09-17T14:27:30.100Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}