fix: deep-copy audio buffers and add mutex to prevent race conditions in autosave

Co-authored-by: aider (deepseek/deepseek-coder) <aider@aider.chat>
This commit is contained in:
Loic Coenen
2026-05-04 12:14:15 +00:00
parent 99bd840fbe
commit e17e40e28f
2 changed files with 47 additions and 2 deletions

21
fs.c
View File

@@ -74,6 +74,19 @@ static void* autosave_thread_func(void *arg) {
continue;
}
// Deep-copy the audio buffers to avoid race conditions
float *buffer_copies[MAX_CLIPS] = {NULL};
for (int i = 0; i < MAX_CLIPS; i++) {
if (state.clips[i].buffer != NULL && state.clips[i].buffer_size > 0) {
buffer_copies[i] = (float *)malloc(state.clips[i].buffer_size * sizeof(float));
if (buffer_copies[i]) {
memcpy(buffer_copies[i], state.clips[i].buffer, state.clips[i].buffer_size * sizeof(float));
// Temporarily point to our copy
state.clips[i].buffer = buffer_copies[i];
}
}
}
// Generate autosave filename with timestamp
char filename[512];
time_t t = time(NULL);
@@ -87,6 +100,14 @@ static void* autosave_thread_func(void *arg) {
fs_save_project(filename, &state);
last_autosave_time = now;
// Free the buffer copies
for (int i = 0; i < MAX_CLIPS; i++) {
if (buffer_copies[i]) {
free(buffer_copies[i]);
buffer_copies[i] = NULL;
}
}
// Remove old autosaves (keep last 10)
DIR *d = opendir(AUTOSAVE_DIR);
if (d) {