Skip to content

Sync-leaderboard.js uses process.exit(1) in helper functions creating unreachable code and aborting cleanup #279

Description

@rishab11250

Description

The processTimeframe() function calls process.exit(1) in two places inside try/catch blocks (lines 321 and 374). This kills the entire Node.js process immediately, bypassing any cleanup that happens after the function returns — including the atomicWrite tmp file cleanup at the top of the main execution block.

Additionally, lines 376 has a return data; statement that is unreachable because line 374 process.exit(1) terminates the process before it can execute.

Affected Code

async function processTimeframe(sourceData, DATA_DIR, periodName, daysAgo, badgesMap) {
  // ...
  try {
    atomicWrite(filepath, data);
    console.log(`${periodName} data saved successfully`);
    return data;        // ← line 370 — fine
  } catch (err) {
    console.error(`Failed to write json file: `, err.message);
    process.exit(1);    // ← line 374 — kills process, line 376 never runs
  }
  return data;          // ← line 376 — unreachable
}

Similarly on line 321:

} catch (err) {
  console.error(`Failed to load previous file: `, err.message);
  process.exit(1);      // ← kills process on a transient file error
}

Steps to Reproduce

  1. Delete one of the daily snapshot files in the data directory.
  2. Run node scripts/sync-leaderboard.js
  3. The script immediately exits with process.exit(1) when it fails to load the previous daily file.
  4. The overall.json may have already been partially written, leaving data in an inconsistent state.

Expected Behavior

  • Use throw err instead of process.exit(1) so errors propagate to the top-level handler.
  • The caller should handle the error, log it, and call process.exit(1) once at the top level if needed.
  • Remove the unreachable return data; after process.exit(1).

Suggested Fix

Replace process.exit(1) with throw err in both locations inside processTimeframe():

} catch (err) {
  console.error(`Failed to write json file: `, err.message);
  throw err;  // instead of process.exit(1)
}

And remove the unreachable return data; at line 376.

Affected Files

  • scripts/sync-leaderboard.js (lines 319-376)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions