Skip to content

Commit

Permalink
Write SDK summary to a temp file and rename (#3740)
Browse files Browse the repository at this point in the history
Avoid writing partial output to the file that could be concurrently read
by another isolate in the same process. Create a temp directory to
incrementally write the file, then use `rename` to atomically move it to
the location where other isolates may try to read it.

It's best to still avoid using the SDK summary from and analysis driver
in multiple isolates, but this should reduce flakiness in some cases
where they do run concurrently.
  • Loading branch information
natebosch authored Aug 26, 2024
1 parent d2a803e commit 15ba22b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 2 additions & 0 deletions build_resolvers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
- Require the latest analyzer, and stop passing the `withNullability`
parameter which was previously required and is now deprecated.
- Bump the min sdk to 3.5.0.
- Fix SDK summary reads when multiple isolates are using build resolvers (not
recommended).

## 2.4.2

Expand Down
9 changes: 7 additions & 2 deletions build_resolvers/lib/src/sdk_summary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,23 @@ Future<String> defaultSdkSummaryGenerator() async {
if (needsRebuild) {
var watch = Stopwatch()..start();
_logger.info('Generating SDK summary...');
await summaryFile.create(recursive: true);
await Directory(cacheDir).create(recursive: true);
final tempDir = await Directory(cacheDir).createTemp();
final tempFile = File(p.join(tempDir.path, p.basename(summaryPath)));
await tempFile.create();
final embedderYamlPath =
isFlutter ? p.join(_dartUiPath, '_embedder.yaml') : null;
await summaryFile.writeAsBytes(
await tempFile.writeAsBytes(
await buildSdkSummary(
sdkPath: _runningDartSdkPath,
resourceProvider: PhysicalResourceProvider.INSTANCE,
embedderYamlPath: embedderYamlPath,
),
);

await tempFile.rename(summaryPath);
await _createDepsFile(depsFile, currentDeps);
await tempDir.delete();
watch.stop();
_logger.info('Generating SDK summary completed, took '
'${humanReadable(watch.elapsed)}\n');
Expand Down

0 comments on commit 15ba22b

Please sign in to comment.