Skip to content

Commit

Permalink
Fixed NRE in UpdateCurrentWorkspaceFolder (#95)
Browse files Browse the repository at this point in the history
CLOSE
https://linear.app/sourcegraph/issue/CODY-3767/updatecurrentworkspacefolder-fails

### Test Plan

1. Open VS with src\Cody.VisualStudio.Tests\TestProjects\ConsoleApp1
solution
2. No files are opened, Cody chat is not selected, Close VS.
3. Open VS again with the same solution (no errors should be visible in
the "Cody" output)
  • Loading branch information
PiotrKarczmarz authored Sep 19, 2024
1 parent 5eae5dc commit 60aa56f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 42 deletions.
45 changes: 25 additions & 20 deletions src/Cody.Core/Infrastructure/WebViewsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ public class WebViewsManager : IWebViewsManager, IDisposable
private readonly INotificationHandler _notificationHandler;
private readonly ILog _logger;

private List<IWebChatHost> _chatHosts;
private List<WebViewEvent> _processedWebViewsRequests;
private readonly List<IWebChatHost> _chatHosts;
private readonly List<WebViewEvent> _processedWebViewsRequests;

private readonly TimeSpan _agentInitializationTimeout = TimeSpan.FromMinutes(1);

private readonly BlockingCollection<WebViewEvent> _events; //TODO: when custom editors will be introduced, make it richer, like BlockingCollection<WebViewsEvents>, where WebViewsEvents will be a class

Expand Down Expand Up @@ -95,24 +97,7 @@ private async Task ProcessEvents()

try
{
var startTime = DateTime.Now;
var timeout = TimeSpan.FromMinutes(1);
while (!_agentProxy.IsInitialized)
{
_logger.Debug("Waiting for Agent initialization ...");
await Task.Delay(TimeSpan.FromSeconds(1));

var nowTime = DateTime.Now;
var currentSpan = nowTime - startTime;
if (currentSpan >= timeout)
{
var message = $"Agent initialization timeout! Waiting for more than {currentSpan.TotalSeconds} s.";
_logger.Error(message);

throw new Exception(message);
}
}

await WaitForAgentInitialization();
await _agentService.ResolveWebviewView(new ResolveWebviewViewParams
{
// cody.chat for sidebar view, or cody.editorPanel for editor panel
Expand Down Expand Up @@ -143,6 +128,26 @@ await _agentService.ResolveWebviewView(new ResolveWebviewViewParams
}
}

private async Task WaitForAgentInitialization()
{
var startTime = DateTime.Now;
while (!_agentProxy.IsInitialized)
{
_logger.Debug("Waiting for Agent initialization ...");
await Task.Delay(TimeSpan.FromSeconds(1));

var nowTime = DateTime.Now;
var currentSpan = nowTime - startTime;
if (currentSpan >= _agentInitializationTimeout)
{
var message = $"Agent initialization timeout! Waiting for more than {currentSpan.TotalSeconds} s.";
_logger.Error(message);

throw new Exception(message);
}
}
}

private void OnRegisterWebViewRequestHandler(object sender, string viewId)
{
try
Expand Down
47 changes: 25 additions & 22 deletions src/Cody.VisualStudio/CodyPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke

InitializeServices();
await InitOleMenu();
await InitializeAgent();

InitializeAgent();

}
catch (Exception ex)
Expand Down Expand Up @@ -365,33 +366,35 @@ private ExtensionConfiguration GetConfiguration()
return config;
}

private async Task InitializeAgent()
private void InitializeAgent()
{
try
{
PrepareAgentConfiguration();

_ = Task.Run(() => AgentClient.Start())
.ContinueWith(async x =>
{
var clientConfig = GetClientInfo();
AgentService = await AgentClient.Initialize(clientConfig);
WebViewsManager.SetAgentService(AgentService);
NotificationHandlers.SetAgentClient(AgentService);
ProgressNotificationHandlers.SetAgentService(AgentService);
})
.ContinueWith(x =>
{
if (SolutionService.IsSolutionOpen()) OnAfterBackgroundSolutionLoadComplete();
SolutionEvents.OnAfterBackgroundSolutionLoadComplete += OnAfterBackgroundSolutionLoadComplete;
SolutionEvents.OnAfterCloseSolution += OnAfterCloseSolution;
})
.ContinueWith(t =>
_ = Task.Run(async () =>
{
foreach (var ex in t.Exception.Flatten().InnerExceptions)
Logger.Error("Agent connecting error", ex);
}, TaskContinuationOptions.OnlyOnFaulted);
try
{
AgentClient.Start();
var clientConfig = GetClientInfo();
AgentService = await AgentClient.Initialize(clientConfig);
WebViewsManager.SetAgentService(AgentService);
NotificationHandlers.SetAgentClient(AgentService);
ProgressNotificationHandlers.SetAgentService(AgentService);
if (SolutionService.IsSolutionOpen()) OnAfterBackgroundSolutionLoadComplete();
SolutionEvents.OnAfterBackgroundSolutionLoadComplete += OnAfterBackgroundSolutionLoadComplete;
SolutionEvents.OnAfterCloseSolution += OnAfterCloseSolution;
}
catch (Exception ex)
{
Logger.Error("Agent initialization failed.", ex);
}
});
}
catch (Exception ex)
{
Expand Down

0 comments on commit 60aa56f

Please sign in to comment.