forked from microsoft/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextHostStoragePaths.ts
More file actions
288 lines (250 loc) · 8.73 KB
/
extHostStoragePaths.ts
File metadata and controls
288 lines (250 loc) · 8.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as fs from 'fs';
import * as path from 'vs/base/common/path';
import { URI } from 'vs/base/common/uri';
import { ExtensionStoragePaths as CommonExtensionStoragePaths } from 'vs/workbench/api/common/extHostStoragePaths';
import { Disposable } from 'vs/base/common/lifecycle';
import { Schemas } from 'vs/base/common/network';
import { IntervalTimer, timeout } from 'vs/base/common/async';
import { ILogService } from 'vs/platform/log/common/log';
import { Promises } from 'vs/base/node/pfs';
export class ExtensionStoragePaths extends CommonExtensionStoragePaths {
private _workspaceStorageLock: Lock | null = null;
protected override async _getWorkspaceStorageURI(storageName: string): Promise<URI> {
const workspaceStorageURI = await super._getWorkspaceStorageURI(storageName);
if (workspaceStorageURI.scheme !== Schemas.file) {
return workspaceStorageURI;
}
if (this._environment.skipWorkspaceStorageLock) {
this._logService.info(`Skipping acquiring lock for ${workspaceStorageURI.fsPath}.`);
return workspaceStorageURI;
}
const workspaceStorageBase = workspaceStorageURI.fsPath;
let attempt = 0;
do {
let workspaceStoragePath: string;
if (attempt === 0) {
workspaceStoragePath = workspaceStorageBase;
} else {
workspaceStoragePath = (
/[/\\]$/.test(workspaceStorageBase)
? `${workspaceStorageBase.substr(0, workspaceStorageBase.length - 1)}-${attempt}`
: `${workspaceStorageBase}-${attempt}`
);
}
await mkdir(workspaceStoragePath);
const lockfile = path.join(workspaceStoragePath, 'vscode.lock');
const lock = await tryAcquireLock(this._logService, lockfile, false);
if (lock) {
this._workspaceStorageLock = lock;
process.on('exit', () => {
lock.dispose();
});
return URI.file(workspaceStoragePath);
}
attempt++;
} while (attempt < 10);
// just give up
return workspaceStorageURI;
}
override onWillDeactivateAll(): void {
// the lock will be released soon
this._workspaceStorageLock?.setWillRelease(6000);
}
}
async function mkdir(dir: string): Promise<void> {
try {
await Promises.stat(dir);
return;
} catch {
// doesn't exist, that's OK
}
try {
await Promises.mkdir(dir, { recursive: true });
} catch {
}
}
const MTIME_UPDATE_TIME = 1000; // 1s
const STALE_LOCK_TIME = 10 * 60 * 1000; // 10 minutes
class Lock extends Disposable {
private readonly _timer: IntervalTimer;
constructor(
private readonly logService: ILogService,
private readonly filename: string
) {
super();
this._timer = this._register(new IntervalTimer());
this._timer.cancelAndSet(async () => {
const contents = await readLockfileContents(logService, filename);
if (!contents || contents.pid !== process.pid) {
// we don't hold the lock anymore ...
logService.info(`Lock '${filename}': The lock was lost unexpectedly.`);
this._timer.cancel();
}
try {
await Promises.utimes(filename, new Date(), new Date());
} catch (err) {
logService.error(err);
logService.info(`Lock '${filename}': Could not update mtime.`);
}
}, MTIME_UPDATE_TIME);
}
public override dispose(): void {
super.dispose();
try { fs.unlinkSync(this.filename); } catch (err) { }
}
public async setWillRelease(timeUntilReleaseMs: number): Promise<void> {
this.logService.info(`Lock '${this.filename}': Marking the lockfile as scheduled to be released in ${timeUntilReleaseMs} ms.`);
try {
const contents: ILockfileContents = {
pid: process.pid,
willReleaseAt: Date.now() + timeUntilReleaseMs
};
await Promises.writeFile(this.filename, JSON.stringify(contents), { flag: 'w' });
} catch (err) {
this.logService.error(err);
}
}
}
/**
* Attempt to acquire a lock on a directory.
* This does not use the real `flock`, but uses a file.
* @returns a disposable if the lock could be acquired or null if it could not.
*/
async function tryAcquireLock(logService: ILogService, filename: string, isSecondAttempt: boolean): Promise<Lock | null> {
try {
const contents: ILockfileContents = {
pid: process.pid,
willReleaseAt: 0
};
await Promises.writeFile(filename, JSON.stringify(contents), { flag: 'wx' });
} catch (err) {
logService.error(err);
}
// let's see if we got the lock
const contents = await readLockfileContents(logService, filename);
if (!contents || contents.pid !== process.pid) {
// we didn't get the lock
if (isSecondAttempt) {
logService.info(`Lock '${filename}': Could not acquire lock, giving up.`);
return null;
}
logService.info(`Lock '${filename}': Could not acquire lock, checking if the file is stale.`);
return checkStaleAndTryAcquireLock(logService, filename);
}
// we got the lock
logService.info(`Lock '${filename}': Lock acquired.`);
return new Lock(logService, filename);
}
interface ILockfileContents {
pid: number;
willReleaseAt: number | undefined;
}
/**
* @returns 0 if the pid cannot be read
*/
async function readLockfileContents(logService: ILogService, filename: string): Promise<ILockfileContents | null> {
let contents: Buffer;
try {
contents = await Promises.readFile(filename);
} catch (err) {
// cannot read the file
logService.error(err);
return null;
}
try {
return JSON.parse(String(contents));
} catch (err) {
// cannot parse the file
logService.error(err);
return null;
}
}
/**
* @returns 0 if the mtime cannot be read
*/
async function readmtime(logService: ILogService, filename: string): Promise<number> {
let stats: fs.Stats;
try {
stats = await Promises.stat(filename);
} catch (err) {
// cannot read the file stats to check if it is stale or not
logService.error(err);
return 0;
}
return stats.mtime.getTime();
}
function processExists(pid: number): boolean {
try {
process.kill(pid, 0); // throws an exception if the process doesn't exist anymore.
return true;
} catch (e) {
return false;
}
}
async function checkStaleAndTryAcquireLock(logService: ILogService, filename: string): Promise<Lock | null> {
const contents = await readLockfileContents(logService, filename);
if (!contents) {
logService.info(`Lock '${filename}': Could not read pid of lock holder.`);
return tryDeleteAndAcquireLock(logService, filename);
}
if (contents.willReleaseAt) {
let timeUntilRelease = contents.willReleaseAt - Date.now();
if (timeUntilRelease < 5000) {
if (timeUntilRelease > 0) {
logService.info(`Lock '${filename}': The lockfile is scheduled to be released in ${timeUntilRelease} ms.`);
} else {
logService.info(`Lock '${filename}': The lockfile is scheduled to have been released.`);
}
while (timeUntilRelease > 0) {
await timeout(Math.min(100, timeUntilRelease));
const mtime = await readmtime(logService, filename);
if (mtime === 0) {
// looks like the lock was released
return tryDeleteAndAcquireLock(logService, filename);
}
timeUntilRelease = contents.willReleaseAt - Date.now();
}
return tryDeleteAndAcquireLock(logService, filename);
}
}
if (!processExists(contents.pid)) {
logService.info(`Lock '${filename}': The pid ${contents.pid} appears to be gone.`);
return tryDeleteAndAcquireLock(logService, filename);
}
const mtime1 = await readmtime(logService, filename);
const elapsed1 = Date.now() - mtime1;
if (elapsed1 <= STALE_LOCK_TIME) {
// the lock does not look stale
logService.info(`Lock '${filename}': The lock does not look stale, elapsed: ${elapsed1} ms, giving up.`);
return null;
}
// the lock holder updates the mtime every 1s.
// let's give it a chance to update the mtime
// in case of a wake from sleep or something similar
logService.info(`Lock '${filename}': The lock looks stale, waiting for 2s.`);
await timeout(2000);
const mtime2 = await readmtime(logService, filename);
const elapsed2 = Date.now() - mtime2;
if (elapsed2 <= STALE_LOCK_TIME) {
// the lock does not look stale
logService.info(`Lock '${filename}': The lock does not look stale, elapsed: ${elapsed2} ms, giving up.`);
return null;
}
// the lock looks stale
logService.info(`Lock '${filename}': The lock looks stale even after waiting for 2s.`);
return tryDeleteAndAcquireLock(logService, filename);
}
async function tryDeleteAndAcquireLock(logService: ILogService, filename: string): Promise<Lock | null> {
logService.info(`Lock '${filename}': Deleting a stale lock.`);
try {
await Promises.unlink(filename);
} catch (err) {
// cannot delete the file
// maybe the file is already deleted
}
return tryAcquireLock(logService, filename, true);
}