7
mirror of https://gitlab.com/kicad/code/kicad.git synced 2025-04-19 14:11:41 +00:00

Shut down curl operations before global cleanup.

Fixes KICAD-7DP
Fixes https://gitlab.com/kicad/code/kicad/-/issues/17103
This commit is contained in:
Alex Shvartzkop 2024-07-04 22:33:53 +03:00
parent 5b81c60b9e
commit 0a53fc41a7
3 changed files with 47 additions and 3 deletions
common/kicad_curl
include/kicad_curl

View File

@ -3,7 +3,7 @@
*
* Copyright (C) 2015 Mark Roszko <mark.roszko@gmail.com>
* Copyright (C) 2016 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
* Copyright (C) 2015-2021 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 2015-2024 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -30,8 +30,14 @@
#include <ki_exception.h> // THROW_IO_ERROR
static std::shared_mutex s_curlMutex;
static bool s_curlShuttingDown = false;
void KICAD_CURL::Init()
{
s_curlShuttingDown = false;
if( curl_global_init( CURL_GLOBAL_ALL ) != CURLE_OK )
{
THROW_IO_ERROR( "curl_global_init() failed." );
@ -41,10 +47,26 @@ void KICAD_CURL::Init()
void KICAD_CURL::Cleanup()
{
s_curlShuttingDown = true;
std::unique_lock lock( s_curlMutex );
curl_global_cleanup();
}
std::shared_mutex& KICAD_CURL::Mutex()
{
return s_curlMutex;
}
bool KICAD_CURL::IsShuttingDown()
{
return s_curlShuttingDown;
}
std::string GetKicadCurlVersion()
{
return KICAD_CURL::GetVersion();

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Mark Roszko <mark.roszko@gmail.com>
* Copyright (C) 2015-2023 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 2015-2024 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -33,6 +33,7 @@
#include <cstddef>
#include <exception>
#include <sstream>
#include <shared_mutex>
#include <wx/app.h>
#include <build_version.h>
@ -88,6 +89,9 @@ static size_t stream_write_callback( void* aContents, size_t aSize, size_t aNmem
static int xferinfo( void* aProgress, curl_off_t aDLtotal, curl_off_t aDLnow, curl_off_t aULtotal,
curl_off_t aULnow )
{
if( KICAD_CURL::IsShuttingDown() )
return 1; // Should abort the operation
CURL_PROGRESS* progress = static_cast<CURL_PROGRESS*>( aProgress );
curl_off_t curtime = 0;
@ -190,6 +194,12 @@ KICAD_CURL_EASY::~KICAD_CURL_EASY()
int KICAD_CURL_EASY::Perform()
{
std::shared_lock lock( KICAD_CURL::Mutex(), std::try_to_lock );
// If can't lock, we should be in the process of tearing down.
if( !lock )
return CURLE_ABORTED_BY_CALLBACK;
if( m_headers )
curl_easy_setopt( m_CURL, CURLOPT_HTTPHEADER, m_headers );

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015 Mark Roszko <mark.roszko@gmail.com>
* Copyright (C) 2015, 2021 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 2015-2024 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -44,6 +44,7 @@
#include <kicommon.h>
#include <curl/curl.h>
#include <string>
#include <shared_mutex>
// CURL_EXTERN expands to dllimport on MinGW which causes gcc warnings. This really should
// expand to nothing on MinGW.
@ -75,6 +76,17 @@ public:
*/
static void Cleanup();
/**
* Returns the mutex for shared locking when performing curl operations.
* Unique locking is performed when shutting down.
*/
static std::shared_mutex& Mutex();
/**
* Returns true if all curl operations should terminate.
*/
static bool IsShuttingDown();
/**
* Wrapper for curl_version(). Reports back a short string of loaded libraries.
*