Skip to content

Commit 9b2438a

Browse files
authored
fix(cli): failure if account cache is malformed (aws#10887)
Because of concurrent running of integration tests, the account cache (which is supposed to be a JSON file) can be read in a state where it's empty or incompletely written, which fails the JSON parse. If that happens, ignore the error and pretend the cache is empty. Fixes sporadic concurrency issues in the integration tests. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent ddff369 commit 9b2438a

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

packages/aws-cdk/lib/api/aws-auth/account-cache.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ export class AccountAccessKeyCache {
8383
// File doesn't exist or is not readable. This is a cache,
8484
// pretend we successfully loaded an empty map.
8585
if (e.code === 'ENOENT' || e.code === 'EACCES') { return {}; }
86+
// File is not JSON, could be corrupted because of concurrent writes.
87+
// Again, an empty cache is fine.
88+
if (e instanceof SyntaxError) { return {}; }
8689
throw e;
8790
}
8891
}

packages/aws-cdk/test/account-cache.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,14 @@ test(`cache is nuked if it exceeds ${AccountAccessKeyCache.MAX_ENTRIES} entries`
111111
await nukeCache(cacheDir);
112112
}
113113
});
114+
115+
test('cache pretends to be empty if cache file does not contain JSON', async() => {
116+
const { cacheDir, cacheFile, cache } = await makeCache();
117+
try {
118+
await fs.writeFile(cacheFile, '');
119+
120+
await expect(cache.get('abc')).resolves.toEqual(undefined);
121+
} finally {
122+
await nukeCache(cacheDir);
123+
}
124+
});

0 commit comments

Comments
 (0)