[solved] Thread safety issue in `torch::jit::load`

The following code triggers an Access violation - code c0000005 on my computer (Windows 10).

#include <torch/script.h>
#include <torch/torch.h>
#include <thread>
#include <vector>

// custom file-path management code
#include "tools/cpp/runfiles/runfiles.h"
#include "util/runfiles.h"

using bazel::tools::cpp::runfiles::Runfiles;
void main(int argc, char** argv) {
  // custom file-path management code
  std::unique_ptr<Runfiles> runfiles = MakeRunfiles(argv[0]);
  const std::string net_location =

  std::vector<std::thread> threads;
  for (int i = 0; i < 10; ++i) {
    threads.emplace_back([&]() {
      try {
        torch::jit::script::Module m = torch::jit::load(net_location);
      } catch (const c10::Error& e) {
        std::cout << "error loading the net from location " << net_location
                  << "\n"
                  << e.what();

  for (auto& thread : threads) {

The call stack shows it happening somewhere in caffe2::Workspace::bookkeeper

Is the jit::load function non-threadsafe or did I do something wrong?

I think the error might be because I compiled in debug mode while using the release version of the torch library.