Skip to content
This repository was archived by the owner on Nov 4, 2024. It is now read-only.

Commit 072a2b3

Browse files
author
Mikhail Arkhipov
authored
Additional logging to diagnose bultins issue (microsoft#1720)
* Remove stale reference * More logging
1 parent 4b4f063 commit 072a2b3

File tree

5 files changed

+41
-17
lines changed

5 files changed

+41
-17
lines changed

src/Analysis/Ast/Impl/Caching/StubCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public void WriteCachedModule(string filePath, string code) {
110110
if (!string.IsNullOrEmpty(cache)) {
111111
_log?.Log(TraceEventType.Verbose, "Writing cached module: ", cache);
112112
// Don't block analysis on cache writes.
113-
CacheWritingTask = Task.Run(() => _fs.WriteTextWithRetry(cache, code));
113+
CacheWritingTask = Task.Run(() => _fs.WriteTextWithRetry(cache, code, _log));
114114
CacheWritingTask.DoNotWait();
115115
}
116116
}

src/Analysis/Ast/Impl/Modules/BuiltinsPythonModule.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515

1616
using System;
1717
using System.Collections.Generic;
18+
using System.Diagnostics;
1819
using System.Linq;
1920
using Microsoft.Python.Analysis.Specializations;
2021
using Microsoft.Python.Analysis.Types;
2122
using Microsoft.Python.Analysis.Values;
2223
using Microsoft.Python.Core;
2324
using Microsoft.Python.Core.IO;
25+
using Microsoft.Python.Core.Logging;
2426
using Microsoft.Python.Parsing;
2527
using Microsoft.Python.Parsing.Ast;
2628

@@ -52,10 +54,19 @@ protected override void OnAnalysisComplete() {
5254
foreach (var n in GetMemberNames()) {
5355
GetMember(n).GetPythonType<PythonType>()?.MakeReadOnly();
5456
}
55-
5657
base.OnAnalysisComplete();
5758
}
5859

60+
protected override string LoadContent() {
61+
var content = base.LoadContent();
62+
if (string.IsNullOrEmpty(content)) {
63+
const string message = "Unable continue, no builtins module content.";
64+
Services.GetService<ILogger>()?.Log(TraceEventType.Error, message);
65+
throw new InvalidOperationException(message);
66+
}
67+
return content;
68+
}
69+
5970
private void SpecializeTypes() {
6071
var isV3 = Interpreter.LanguageVersion.Is3x();
6172

src/Analysis/Ast/Impl/Modules/CompiledPythonModule.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ private string ScrapeModule() {
100100

101101
output = process.StandardOutput.ReadToEnd();
102102
}
103-
} catch (Exception ex) when (!ex.IsCriticalException()) { }
103+
} catch (Exception ex) when (!ex.IsCriticalException()) {
104+
Log?.Log(TraceEventType.Verbose, "Exception scraping module", Name, ex.Message);
105+
}
104106

105107
return output;
106108
}

src/Analysis/Ast/Impl/Modules/Resolution/MainModuleResolution.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ public IPythonModule GetSpecializedModule(string fullName, bool allowCreation =
164164
public bool IsSpecializedModule(string fullName, string modulePath = null)
165165
=> _specialized.ContainsKey(fullName);
166166

167-
internal async Task AddBuiltinTypesToPathResolverAsync(CancellationToken cancellationToken = default) {
167+
private async Task AddBuiltinTypesToPathResolverAsync(CancellationToken cancellationToken = default) {
168168
var analyzer = Services.GetService<IPythonAnalyzer>();
169-
await analyzer.GetAnalysisAsync(BuiltinsModule, -1, cancellationToken);
169+
await analyzer.GetAnalysisAsync(BuiltinsModule, Timeout.Infinite, cancellationToken);
170170

171-
Check.InvalidOperation(!(BuiltinsModule.Analysis is EmptyAnalysis), "After await");
171+
Check.InvalidOperation(!(BuiltinsModule.Analysis is EmptyAnalysis), "Builtins analysis did not complete correctly.");
172172

173173
// Add built-in module names
174174
var builtinModuleNamesMember = BuiltinsModule.GetAnyMember("__builtin_module_names__");

src/Core/Impl/Extensions/IOExtensions.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@
1414
// permissions and limitations under the License.
1515

1616
using System;
17+
using System.Diagnostics;
1718
using System.IO;
1819
using System.Linq;
1920
using System.Text;
2021
using System.Threading;
22+
using Microsoft.Python.Core.Logging;
2123

2224
namespace Microsoft.Python.Core.IO {
2325
public static class IOExtensions {
@@ -79,7 +81,7 @@ public static bool DeleteDirectoryWithRetries(this IFileSystem fs, string path,
7981
return !fs.DirectoryExists(path);
8082
}
8183

82-
public static FileStream OpenWithRetry(this IFileSystem fs, string file, FileMode mode, FileAccess access, FileShare share) {
84+
public static FileStream OpenWithRetry(this IFileSystem fs, string file, FileMode mode, FileAccess access, FileShare share, ILogger log = null) {
8385
// Retry for up to one second
8486
var create = mode != FileMode.Open;
8587
for (var retries = 100; retries > 0; --retries) {
@@ -89,20 +91,23 @@ public static FileStream OpenWithRetry(this IFileSystem fs, string file, FileMod
8991
return null;
9092
} catch (DirectoryNotFoundException) when (!create) {
9193
return null;
92-
} catch (UnauthorizedAccessException) {
94+
} catch (UnauthorizedAccessException uaex) {
95+
log?.Log(TraceEventType.Verbose, "Unable to open file ", file, uaex.Message);
9396
Thread.Sleep(10);
9497
} catch (IOException) {
9598
if (create) {
9699
var dir = Path.GetDirectoryName(file);
97100
try {
98101
fs.CreateDirectory(dir);
99-
} catch (IOException) {
100-
// Cannot create directory for DB, so just bail out
101-
return null;
102+
} catch (IOException ioex) {
103+
log?.Log(TraceEventType.Verbose, "Unable to create directory ", dir, ioex.Message);
104+
Thread.Sleep(10);
102105
}
106+
} else {
107+
Thread.Sleep(10);
103108
}
104-
Thread.Sleep(10);
105-
} catch (NotSupportedException) {
109+
} catch (NotSupportedException nsx) {
110+
log?.Log(TraceEventType.Verbose, "Unable to open file ", file, nsx.Message);
106111
return null;
107112
}
108113
}
@@ -121,21 +126,27 @@ public static string ReadTextWithRetry(this IFileSystem fs, string file) {
121126
return null;
122127
}
123128

124-
public static void WriteTextWithRetry(this IFileSystem fs, string filePath, string text) {
129+
public static void WriteTextWithRetry(this IFileSystem fs, string filePath, string text, ILogger log = null) {
130+
Exception ex = null;
125131
for (var retries = 100; retries > 0; --retries) {
126132
try {
127-
using (var stream = fs.OpenWithRetry(filePath, FileMode.Create, FileAccess.Write, FileShare.Read)) {
133+
using (var stream = fs.OpenWithRetry(filePath, FileMode.Create, FileAccess.Write, FileShare.Read, log)) {
128134
if (stream != null) {
129135
var bytes = Encoding.UTF8.GetBytes(text);
130136
stream.Write(bytes, 0, bytes.Length);
131137
return;
132138
}
133139
}
134-
} catch (IOException) { } catch (UnauthorizedAccessException) {
135-
Thread.Sleep(10);
140+
} catch (IOException ioex) {
141+
ex = ioex;
142+
} catch (UnauthorizedAccessException uaex) {
143+
ex = uaex;
136144
}
145+
Thread.Sleep(10);
137146
}
138147

148+
log?.Log(TraceEventType.Verbose, "Unable to write to ", filePath, ex?.Message ?? "Unknown exception");
149+
139150
try {
140151
fs.DeleteFile(filePath);
141152
} catch (IOException) { } catch (UnauthorizedAccessException) { }

0 commit comments

Comments
 (0)