Skip to content

Commit d630e29

Browse files
Merge pull request #4276 from PowerShell/andschwa/lock-logger
Make `Logger.WriteLine` thread-safe and fix bug with UNC paths
2 parents 68f35d5 + 40d6d4b commit d630e29

File tree

3 files changed

+18
-4
lines changed

3 files changed

+18
-4
lines changed

src/features/ExternalApi.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ export class ExternalApiFeature extends LanguageClientConsumer implements IPower
169169
}
170170

171171
public getStorageUri(): vscode.Uri {
172-
return this.extensionContext.globalStorageUri;
172+
// We have to override the scheme because it defaults to
173+
// 'vscode-userdata' which breaks UNC paths.
174+
return this.extensionContext.globalStorageUri.with({ scheme: "file"});
173175
}
174176

175177
public dispose() {

src/logging.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ export class Logger implements ILogger {
3535
private logChannel: vscode.OutputChannel;
3636
private logFilePath: vscode.Uri;
3737
private logDirectoryCreated = false;
38+
private writingLog = false;
3839

3940
constructor(logLevelName: string, globalStorageUri: vscode.Uri) {
4041
this.logLevel = Logger.logLevelNameToValue(logLevelName);
4142
this.logChannel = vscode.window.createOutputChannel("PowerShell Extension Logs");
43+
// We have to override the scheme because it defaults to
44+
// 'vscode-userdata' which breaks UNC paths.
4245
this.logDirectoryPath = vscode.Uri.joinPath(
43-
globalStorageUri,
46+
globalStorageUri.with({ scheme: "file" }),
4447
"logs",
4548
`${Math.floor(Date.now() / 1000)}-${vscode.env.sessionId}`);
4649
this.logFilePath = this.getLogFilePath("vscode-powershell");
@@ -194,11 +197,16 @@ export class Logger implements ILogger {
194197
const timestampedMessage = Logger.timestampMessage(message, level);
195198
this.logChannel.appendLine(timestampedMessage);
196199
if (this.logLevel !== LogLevel.None) {
200+
// A simple lock because this function isn't re-entrant.
201+
while (this.writingLog) {
202+
await utils.sleep(300);
203+
}
197204
try {
205+
this.writingLog = true;
198206
if (!this.logDirectoryCreated) {
199207
this.logChannel.appendLine(Logger.timestampMessage(`Creating log directory at: '${this.logDirectoryPath}'`, level));
200208
await vscode.workspace.fs.createDirectory(this.logDirectoryPath);
201-
this.logDirectoryCreated = await utils.checkIfDirectoryExists(this.logDirectoryPath);
209+
this.logDirectoryCreated = true;
202210
}
203211
let log = new Uint8Array();
204212
if (await utils.checkIfFileExists(this.logFilePath)) {
@@ -209,6 +217,8 @@ export class Logger implements ILogger {
209217
Buffer.concat([log, Buffer.from(timestampedMessage)]));
210218
} catch (e) {
211219
console.log(`Error writing to vscode-powershell log file: ${e}`);
220+
} finally {
221+
this.writingLog = false;
212222
}
213223
}
214224
}

src/session.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ export class SessionManager implements Middleware {
111111

112112
// Create the language status item
113113
this.languageStatusItem = this.createStatusBarItem();
114-
this.sessionsFolder = vscode.Uri.joinPath(extensionContext.globalStorageUri, "sessions");
114+
// We have to override the scheme because it defaults to
115+
// 'vscode-userdata' which breaks UNC paths.
116+
this.sessionsFolder = vscode.Uri.joinPath(extensionContext.globalStorageUri.with({ scheme: "file"}), "sessions");
115117
this.platformDetails = getPlatformDetails();
116118
this.HostName = hostName;
117119
this.HostVersion = hostVersion;

0 commit comments

Comments
 (0)