Kaydet (Commit) 9899ffd2 authored tarafından Michael Stahl's avatar Michael Stahl

comphelper: fix MSVC hang in ThreadPool::shutdown()

Commit aa68c99d added some code using
std::condition_variable to comphelper.

Built with MSVC 2017, this causes many cppunittester.exe processes to
deadlock in ThreadPool::shutdown():

    maTasksChanged.notify_all();

This ultimately calls NtReleaseKeyedEvent(), which never returns.

The reason appears to be a bug in Windows 7, for which a "hotfix"[1] is
avaiable here, but it's apparently not distributed via Windows Update
so we likely can't rely on users or even developers having this installed.

However, the documentation of DllMain[2] and ExitProcess[3] indicates
that during shutdown, by the time global destructors are invoked
all threads other than the one that called ExitProcess have already
been terminated.

Returning from main() implicitly calls ExitProcess [4].

As it turns out the problem only happens for some CppUnitTests because
soffice.bin will call ThreadPool::shutdown() from Desktop::doShutdown()
while it is still safe.

[1] http://support.microsoft.com/kb/2582203
[2] https://msdn.microsoft.com/en-US/library/windows/desktop/ms682583(v=vs.85).aspx
[3] https://msdn.microsoft.com/en-us/library/windows/desktop/ms682658(v=vs.85).aspx
[4] https://blogs.msdn.microsoft.com/oldnewthing/20100827-00/?p=13023

Change-Id: I6137461ca7efe9a5fbe4f8f8478fb96de3570469
Reviewed-on: https://gerrit.libreoffice.org/35066Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarMichael Stahl <mstahl@redhat.com>
üst aa204f1c
......@@ -89,7 +89,12 @@ ThreadPool::ThreadPool( sal_Int32 nWorkers ) :
ThreadPool::~ThreadPool()
{
shutdown();
// note: calling shutdown from global variable dtor blocks forever on Win7
// note2: there isn't enough MSVCRT left on exit to call assert() properly
// so these asserts just print something to stderr but exit status is
// still 0, but hopefully they will be more helpful on non-WNT platforms
assert(mbTerminate);
assert(maTasks.empty());
}
struct ThreadPoolStatic : public rtl::StaticWithInit< std::shared_ptr< ThreadPool >,
......@@ -127,6 +132,7 @@ sal_Int32 ThreadPool::getPreferredConcurrency()
return ThreadCount;
}
// FIXME: what does "this" refer to in the following?
// FIXME: there should be no need for this as/when our baseline
// is >VS2015 and drop WinXP; the sorry details are here:
// https://connect.microsoft.com/VisualStudio/feedback/details/1282596
......
......@@ -119,8 +119,16 @@ namespace
uno::Reference<container::XNameAccess> xNA(xZip, uno::UNO_QUERY);
CPPUNIT_ASSERT(xNA.is());
struct TestThreadPool
{
comphelper::ThreadPool aPool(4);
comphelper::ThreadPool aPool;
TestThreadPool(sal_Int32 const i) : aPool(i) {}
~TestThreadPool() { aPool.shutdown(); }
};
{
TestThreadPool aPool(4);
comphelper::ThreadPool & rPool(aPool.aPool);
std::shared_ptr<comphelper::ThreadTaskTag> pTag = comphelper::ThreadPool::createThreadTaskTag();
std::vector<std::vector<char>> aTestBuffers(26);
......@@ -135,10 +143,10 @@ namespace
xNA->getByName(aName) >>= xStrm;
CPPUNIT_ASSERT(xStrm.is());
aPool.pushTask(new Worker(pTag, xStrm, *itBuf));
rPool.pushTask(new Worker(pTag, xStrm, *itBuf));
}
aPool.waitUntilDone(pTag);
rPool.waitUntilDone(pTag);
// Verify the streams.
CPPUNIT_ASSERT_EQUAL(size_t(26), aTestBuffers.size());
......
......@@ -318,7 +318,7 @@ static PyObject* getComponentContext(
}
static PyObject* initTestEnvironment(
SAL_UNUSED_PARAMETER PyObject*, SAL_UNUSED_PARAMETER PyObject*)
SAL_UNUSED_PARAMETER PyObject*, SAL_UNUSED_PARAMETER PyObject* args)
{
// this tries to bootstrap enough of the soffice from python to run
// unit tests, which is only possible indirectly because pyuno is URE
......@@ -349,10 +349,21 @@ static PyObject* initTestEnvironment(
mod.load(OStringToOUString(libname, osl_getThreadTextEncoding()),
SAL_LOADMODULE_LAZY | SAL_LOADMODULE_GLOBAL);
if (!mod.is()) { abort(); }
oslGenericFunction const pFunc(
mod.getFunctionSymbol("test_init"));
if (!pFunc) { abort(); }
reinterpret_cast<void (SAL_CALL *)(XMultiServiceFactory*)>(pFunc)(xMSF.get());
assert(PyTuple_Check(args));
if (PyTuple_Size(args) == 0)
{
oslGenericFunction const pFunc(
mod.getFunctionSymbol("test_init"));
if (!pFunc) { abort(); }
reinterpret_cast<void (SAL_CALL *)(XMultiServiceFactory*)>(pFunc)(xMSF.get());
}
else
{
oslGenericFunction const pFunc(
mod.getFunctionSymbol("test_fini"));
if (!pFunc) { abort(); }
reinterpret_cast<void (SAL_CALL *)()>(pFunc)();
}
}
catch (const css::uno::Exception &)
{
......
......@@ -16,6 +16,7 @@
#include <rtl/bootstrap.hxx>
#include <cppuhelper/bootstrap.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/threadpool.hxx>
#include <com/sun/star/lang/Locale.hpp>
#include <com/sun/star/lang/XComponent.hpp>
......@@ -97,6 +98,12 @@ SAL_DLLPUBLIC_EXPORT void test_init(lang::XMultiServiceFactory *pFactory)
catch (...) { abort(); }
}
// this is called from pyuno
SAL_DLLPUBLIC_EXPORT void test_fini()
{
::comphelper::ThreadPool::getSharedOptimalPool().shutdown();
}
} // extern "C"
void test::BootstrapFixture::setUp()
......
......@@ -14,6 +14,7 @@
#include <sal/types.h>
#include <test/setupvcl.hxx>
#include <vcl/svapp.hxx>
#include <comphelper/threadpool.hxx>
#include <isheadless.hxx>
......@@ -28,6 +29,8 @@ public:
private:
virtual ~Protector() override {
DeInitVCL();
// for the 6 tests that use it
comphelper::ThreadPool::getSharedOptimalPool().shutdown();
}
virtual bool protect(
......
......@@ -182,6 +182,9 @@ class UnoInProcess:
global havePonies
if not(havePonies):
pyuno.private_initTestEnvironment()
# note: this will be called early enough, from Py_Finalize
import atexit
atexit.register(pyuno.private_initTestEnvironment, False)
havePonies = True
def openEmptyWriterDoc(self):
assert(self.xContext)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment