Skip to content

ResourceManagerUtils.DownloadResource aquires mutex on one thread and releases from another #6980

Open

Description

@ericstj

System Information (please complete the following information):

  • OS & Version: [e.g. Windows 10] Win 11
  • ML.NET Version: [e.g. ML.NET v1.5.5] latest
  • .NET Version: [e.g. .NET 5.0] .NET 6.0

Describe the bug
I noticed an exception during local testing. The error from the test was DownloadFailed with exception One or more errors occurred. (Object synchronization method was called from an unsynchronized block of code.) This is happening because we are using a Mutex within an async method.

Mutex's have thread affinity. They must be released from the same thread that they were acquired from:
https://learn.microsoft.com/en-us/dotnet/api/system.threading.mutex.releasemutex?view=net-8.0

However an async method that uses ConfigureAwait(false) will not necessarily resume on the same thread.

To Reproduce
Steps to reproduce the behavior:

  1. Delete local copies of ML.NET resources (eg from %TEMP%\mlnet)
  2. Run dotnet test on Microsoft.ML.TorchSharp.Tests
  3. Observe failure, if not observed then repeat from step 1.

Expected behavior
Tests run to completion.

Screenshots, Code, Sample Projects

System.InvalidOperationException : Error downloading resource from 'https://aka.ms/mlnet-resources/models/pretrained_Roberta_encoder.tsm': DownloadFailed with exception One or more errors occurred. (Object synchronization method was called from an unsynchronized block of code.)\\nDownloadFailed with exception One or more errors occurred. (A task was canceled.)\\nDownloadFailed with exception One or more errors occurred. (The wait completed due to an abandoned mutex.)\\nDownloadFailed with exception One or more errors occurred. (A task was canceled.)\\nDownloadFailed with exception One or more errors occurred. (The wait completed due to an abandoned mutex.)\\n\nmodel file could not be downloaded!
   at Microsoft.ML.TorchSharp.Roberta.QATrainer.Trainer.GetModelPath() in C:\src\dotnet\machinelearning\src\Microsoft.ML.TorchSharp\Roberta\QATrainer.cs:line 260

Activity

  1. ghost added
    untriagedNew issue has not been triaged
    on Jan 31, 2024
  2. added this to the ML.NET 4.0 milestone on Feb 7, 2024
  3. ghost removed
    untriagedNew issue has not been triaged
    on Feb 7, 2024
  4. changed the title [-]ResourceManagerUtils.DownloadResource aquires mutex on one thread and releases from antother[/-] [+]ResourceManagerUtils.DownloadResource aquires mutex on one thread and releases from another[/+] on Feb 8, 2024
  5. modified the milestones: ML.NET 4.0, ML.NET 5.0 on Jan 16, 2025
  6. augustevn commented on Dec 12, 2025

    @augustevn

    Still happening on the QuestionAnswer trainer.

    Packages & versions:

    Microsoft.Data.Analysis, 0.23.0
    Microsoft.ML, 5.0.0
    
    Microsoft.ML.TorchSharp, 0.23.0
    TorchSharp-cuda-windows, 0.105.2
    

    I appreciate the effort you put into making nice API's but sooner rather than later, we'll need to be able to use different models for i.e. embedding generation or multilingual scenarios. Having APIs for that would be awesome. For now, I'll follow the Python to ONNX approaches.

    & clarity on CUDA compatibility would help a ton! It works in Python but not in my Interactive (Polyglot) notebooks.

    For now, the newest GloVe resolved my Semantic Search (Match) case.

  7. added
    area-CoreCore ML.NET APIs and shared functionality
    on Sep 8, 2026
  8. rosebyte commented on Oct 22, 2026

    @rosebyte
    Member

    Triage: it's a bug, we will address it when we have time. It's hitting even our tests sporadically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CoreCore ML.NET APIs and shared functionalitygreendevelopment

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions