Skip to content

Advanced Paste: Handle Foundry local Port change on the fly#45362

Merged
vanzue merged 1 commit intomainfrom
dev/vanzue/fl
Feb 10, 2026
Merged

Advanced Paste: Handle Foundry local Port change on the fly#45362
vanzue merged 1 commit intomainfrom
dev/vanzue/fl

Conversation

@vanzue
Copy link
Copy Markdown
Contributor

@vanzue vanzue commented Feb 4, 2026

Summary of the Pull Request

Foundry Local returns 400 Bad Request if a manual port change made for foundry local.

Fix #45340

PR Checklist

  • Communication: I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected
  • Tests: Added/updated and all pass
  • Localization: All end-user-facing strings can be localized
  • Dev docs: Added/updated
  • New binaries: Added on the required places
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

Validation Steps Performed

Follow steps described in the issue, and the advanced paste can work without having to restart powertoys itself

…client to make sure our client function well
@vanzue vanzue requested a review from Copilot February 4, 2026 14:49
@vanzue vanzue marked this pull request as ready for review February 4, 2026 14:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the Foundry Local provider used by Advanced Paste to better tolerate Foundry Local endpoint/port changes at runtime by refreshing the underlying client and re-resolving the service URL when failures occur.

Changes:

  • Refreshes the Foundry Local client when the model isn’t found in the catalog (to handle stale catalog/service state).
  • Retries EnsureModelLoaded with a client refresh on failure/exception.
  • Re-fetches service URL during initialization and refreshes when ServiceUri is unavailable.

Comment on lines +227 to +231
_foundryClient = null;
_catalogModels = null;
_serviceUrl = null;

InitializeAsync().GetAwaiter().GetResult();
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TryRefreshClient mutates the singleton’s shared state (_foundryClient/_catalogModels/_serviceUrl) without any synchronization. Since this provider is used from both Settings UI and Advanced Paste, concurrent calls (e.g., GetModelsAsync while a paste action triggers a refresh) can race and produce null dereferences/inconsistent reads. Consider guarding initialization/refresh and all reads of these fields with a lock/SemaphoreSlim, or swapping an immutable state object via Interlocked.Exchange so callers always operate on a consistent snapshot.

Copilot uses AI. Check for mistakes.
Logger.LogWarning($"[FoundryLocal] Model not found in catalog. Refreshing client for model: {modelId}");
if (!TryRefreshClient("Model not in catalog"))
{
return false;
Copy link

Copilot AI Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EnsureModelInCatalog returns false both when the model truly isn’t in the catalog and when refreshing the Foundry client fails. In the refresh-failure case, GetIChatClient will currently throw the "model is not supported" InvalidOperationException, which is misleading (the real issue may be that Foundry Local isn’t reachable/initialized). Consider propagating the refresh failure separately (e.g., throw a more accurate InvalidOperationException when TryRefreshClient fails, or return a result that preserves the failure reason).

Suggested change
return false;
const string message = "Foundry Local client could not be refreshed. Please make sure Foundry Local is installed and running.";
Logger.LogError($"[FoundryLocal] {message}");
throw new InvalidOperationException(message);

Copilot uses AI. Check for mistakes.
@vanzue vanzue merged commit 67a013f into main Feb 10, 2026
20 of 21 checks passed
@vanzue vanzue deleted the dev/vanzue/fl branch February 10, 2026 07:31
@zateutsch zateutsch added this to the PowerToys 0.98 milestone Mar 4, 2026
@zateutsch zateutsch added the Product-Advanced Paste Refers to the Advanced Paste module label Mar 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Product-Advanced Paste Refers to the Advanced Paste module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Advanced Paste: Foundry Local returns 400 Bad Request despite model being loaded and working

4 participants