Skip to content

Integrate Clang ThreadSanitizer in tests#1986

Open
3manifold wants to merge 2 commits intoOpenNMT:masterfrom
3manifold:tsan
Open

Integrate Clang ThreadSanitizer in tests#1986
3manifold wants to merge 2 commits intoOpenNMT:masterfrom
3manifold:tsan

Conversation

@3manifold
Copy link
Contributor

@3manifold 3manifold commented Jan 22, 2026

Description

Integrate TSAN similarly to #1903

resolves #1987

P.S. To avoid confusion with earlier PRs, #1993 had incorrect title; the correct one should be "Use LLVM ThreadSanitizerAddressSanitizer instead of Google".

Test case

One can introduce a race condition to test tsan by modifying include/ctranslate2/ops/mul.h and run tests again. That is:

#pragma once

#include "op.h"
#include <iostream>
#include <thread>
namespace ctranslate2 {
  namespace ops {

    class Mul : public BinaryOp {
    public:
      void operator()(const StorageView& a, const StorageView& b, StorageView& c) const override;
      mutable int store_items = 20;
      mutable int shop_items = 0;
 
      void transfer_items(int amount, int& out, int& in) const
      {
        out -= amount;
        std::this_thread::sleep_for(std::chrono::nanoseconds(10));
        in += amount;
      }
      
    private:
      template <Device D, typename T>
      void compute(const StorageView& a, const StorageView& b, StorageView& c) const {

        std::thread t1( &Mul::transfer_items, this, 5, std::ref(store_items), std::ref(shop_items) );
        std::thread t2( &Mul::transfer_items, this, 2, std::ref(shop_items), std::ref(store_items) );

        t1.join();
        t2.join();

        c.resize_as(a);
        if (b.is_scalar()) {
          primitives<D>::mul(b.data<T>()[0], a.data<T>(), c.data<T>(), c.size());
        } else {
          primitives<D>::mul(a.data<T>(), b.data<T>(), c.data<T>(), c.size());
        }
      }
    };

  }
}

@3manifold 3manifold marked this pull request as ready for review January 22, 2026 09:32
@3manifold 3manifold marked this pull request as draft January 27, 2026 16:11
@3manifold 3manifold force-pushed the tsan branch 6 times, most recently from f36080f to f5cecc4 Compare January 30, 2026 14:48
env:
CT2_USE_MKL: 1
run: |
TSAN_OPTIONS="ignore_noninstrumented_modules=1:halt_on_error=1" tests/ctranslate2_test tests/data
Copy link
Contributor Author

@3manifold 3manifold Feb 4, 2026

Choose a reason for hiding this comment

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

ignore_noninstrumented_modules flag which makes ThreadSanitizer ignore all interceptors called from the given set of (non-instrumented) libraries

https://github.com/google/sanitizers/wiki/ThreadSanitizerCppManual#non-instrumented-code

This avoids false positives coming mostly from OpenMP.

@3manifold 3manifold marked this pull request as ready for review February 4, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clang ThreadSanitizer in CI

1 participant