Skip to content

Fix race condition in learning objects storage persistence #6

@meanmail

Description

@meanmail

Overview

YAML serialization can race with file content persistence, potentially causing data corruption or inconsistency.

Location

File: intellij-plugin/hs-core/src/org/hyperskill/academy/learning/storage/LearningObjectsStorageManager.kt
Function: EduFile.persist (lines 42-91)

Problem

The code persists file contents on a pooled thread but doesn't properly synchronize YAML serialization. The comment on lines 137-138 says "This should be called before YAML serialization to avoid race conditions" but there's no enforcement mechanism.

// This should be called before YAML serialization to avoid race conditions
fun waitForPersistingTasks() {
    // Called manually but no guarantee it happens before save
}

What needs to be fixed

  • The waitForPersistingTasks() function (lines 140-152) is called manually but there's no guarantee it's called before YAML serialization
  • Add a listener or hook to ensure waitForPersistingTasks() is automatically called before YAML serialization
  • Consider using CompletableFuture instead of raw Future for better composition
  • Add proper error handling for cases where contents change during persistence (lines 72-85)

Tests understanding of

  • Concurrent programming and race conditions
  • Future/CompletableFuture patterns
  • IntelliJ's message bus system or save listeners
  • File content synchronization strategies

Validation

  • Write a test that rapidly modifies files and triggers saves
  • Ensure no corruption or data loss occurs
  • Verify that persistence completes before YAML serialization

Estimated time

4-5 hours

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions