Initialization: set Boost path locale in main thread #5877

pull dexX7 wants to merge 1 commits into bitcoin:master from dexX7:test-path-multithread-deinit-guard changing 1 files +9 −9
  1. dexX7 commented at 3:34 pm on March 11, 2015: contributor
    1. Boost path seems to use variables which are lazy initialized and not thread safe, and in particular this results in leaks and unexpected behavior, if they are not explicitly initialized by the main thread, because resources may no longer be accessible, if the related thread isn’t either. This is basically the underlying issue I’m trying to address, and related issues affected by this are #3136 and #5380.
    2. path::imbue(std::locale()) appears to initialize those.
    3. std::locale() constructs a copy of the current global locale. The global locale is the locale set by a previous call to std::locale::global, or std::locale::classic (“C”), if std::locale::global has not been called. std::locale("") however uses the default environment value.
    4. Setting locale::global(loc) also sets the C global locale, as if the C library function setlocale was called with LC_ALL, which appears to be more robust, while setenv may not be available in any case.
    5. locale("") throw exceptions, if invalid environment locales are used, but locale() does only so, if locale::global was set with an invalid locale before, which should not be the case in Bitcoin Core, so I’d assume the old L732 never throws at all.
    6. I tested it with several build configurations, but I’m not entirely sure, if this is 100 % side effect free.
  2. Initialization: set Boost path locale in main thread
    The path locale is lazy initialized and to avoid deinitialization errors
    in multithreading environments, it is set explicitly by the main thread.
    317e66c741
  3. laanwj added the label Improvement on Mar 11, 2015
  4. in src/util.cpp: in 317e66c741
    722@@ -723,18 +723,18 @@ void RenameThread(const char* name)
    723 
    724 void SetupEnvironment()
    725 {
    726-#ifndef WIN32
    727-    try
    728-    {
    729-#if BOOST_FILESYSTEM_VERSION == 3
    730-            boost::filesystem::path::codecvt(); // Raises runtime error if current locale is invalid
    


    laanwj commented at 4:42 pm on March 11, 2015:
    Are you sure this will still work for boost filesystem v3? I’d assume this #ifdef construction was added for a reason.

    dexX7 commented at 1:38 pm on March 12, 2015:
    Yes, it’s actually std::locale("") what raises the exception, which is called from within path::codecvt().
  5. laanwj commented at 2:55 pm on March 20, 2015: member
    Tested ACK (tested that SetupEnvironment still does what it should, not tested that #3136 / #5380 don’t occur anymore - can you try @diapolo?).
  6. jonasschnelli commented at 3:01 pm on March 21, 2015: contributor
    Tested. ACK. #3136 is fixed with this PR. #5380 still occurs (OSX 10.10). I don’t see a relation between this PR and #5380. Multiple leaks detected (could be not real leaks because of BDB style of handling memory): bildschirmfoto 2015-03-21 um 15 57 10
  7. Diapolo commented at 5:36 pm on March 21, 2015: none
    Tested ACK, #3136 is fixed with this PR :), great!
  8. laanwj merged this on Mar 24, 2015
  9. laanwj closed this on Mar 24, 2015

  10. laanwj referenced this in commit 28cc24f961 on Mar 24, 2015
  11. laanwj commented at 7:46 am on March 24, 2015: member
    Cherry-picked to 0.10 as c9e022b7eee9f77cd33b333d6ff52f711d3bc989
  12. laanwj referenced this in commit c9e022b7ee on Mar 24, 2015
  13. in src/util.cpp: in 317e66c741
    741+        std::locale::global(std::locale("C"));
    742     }
    743 #endif
    744+    // The path locale is lazy initialized and to avoid deinitialization errors 
    745+    // in multithreading environments, it is set explicitly by the main thread.
    746+    boost::filesystem::path::imbue(std::locale());    
    


    jonasschnelli commented at 11:28 am on May 1, 2015:
    This line is the reason for #6078
  14. reddink referenced this in commit 66c4b44c75 on May 27, 2020
  15. DrahtBot locked this on Sep 8, 2021

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-12-18 18:12 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me