From 89d209096241185fb021a26cad613cc9c4042264 Mon Sep 17 00:00:00 2001 From: Jaret Burkett Date: Wed, 25 Mar 2026 11:22:42 -0600 Subject: [PATCH] Fixed race condition that would occasionally set the dataset path to the first one when editing a job --- ui/src/app/jobs/new/page.tsx | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/ui/src/app/jobs/new/page.tsx b/ui/src/app/jobs/new/page.tsx index bbe8042b..d0a2a85c 100644 --- a/ui/src/app/jobs/new/page.tsx +++ b/ui/src/app/jobs/new/page.tsx @@ -6,7 +6,7 @@ import { defaultJobConfig, defaultDatasetConfig, migrateJobConfig } from './jobC import { jobTypeOptions } from './options'; import { JobConfig } from '@/types'; import { objectCopy } from '@/utils/basic'; -import { useNestedState } from '@/utils/hooks'; +import { useNestedState, setNestedValue } from '@/utils/hooks'; import { SelectInput } from '@/components/formInputs'; import useSettings from '@/hooks/useSettings'; import useGPUInfo from '@/hooks/useGPUInfo'; @@ -87,15 +87,19 @@ export default function TrainingForm() { const datasetOptions = datasets.map(name => ({ value: path.join(settings.DATASETS_FOLDER, name), label: name })); setDatasetOptions(datasetOptions); - const defaultDatasetPath = defaultDatasetConfig.folder_path; - for (let i = 0; i < jobConfig.config.process[0].datasets.length; i++) { - const dataset = jobConfig.config.process[0].datasets[i]; - if (dataset.folder_path === defaultDatasetPath) { - if (datasetOptions.length > 0) { - setJobConfig(datasetOptions[0].value, `config.process[0].datasets[${i}].folder_path`); + if (datasetOptions.length > 0) { + const defaultDatasetPath = defaultDatasetConfig.folder_path; + // Use functional updater so we check the *current* state, not a stale closure + setJobConfig((prev: JobConfig) => { + let updated = prev; + for (let i = 0; i < prev.config.process[0].datasets.length; i++) { + if (prev.config.process[0].datasets[i].folder_path === defaultDatasetPath) { + updated = setNestedValue(updated, datasetOptions[0].value, `config.process[0].datasets[${i}].folder_path`); + } } - } + return updated; + }); } }, [datasets, settings, isSettingsLoaded, datasetFetchStatus]);