Fix Cancel() semantics under MSW and other improvements to it

Under MSW, don't set the state to State_Cancelled as soon as Cancel()
was called, as the request was still used from the other threads
afterwards, resulting in race conditions and crashes.

Fix this by just removing the SetState(State_Cancelled) call from the
main thread, as it was redundant anyhow. This also makes the behaviour
correspond to the documentation, which indicates that Cancel() works
asynchronously.

Also ensure, for all backends, that we actually cancel the request only
once, even if public Cancel() is called multiple times. This required
renaming the existing wxWebRequestImpl::Cancel() to DoCancel().
This commit is contained in:
Vadim Zeitlin
2021-01-16 13:49:22 +01:00
parent b428e531ee
commit 508a4f6ca8
9 changed files with 49 additions and 21 deletions

View File

@@ -72,7 +72,9 @@ public:
// Precondition for this method checked by caller: current state is idle.
virtual void Start() = 0;
virtual void Cancel() = 0;
// Precondition for this method checked by caller: not idle and not already
// cancelled.
void Cancel();
virtual wxWebResponseImplPtr GetResponse() const = 0;
@@ -114,6 +116,8 @@ protected:
wxEvtHandler* handler,
int id);
bool WasCancelled() const { return m_cancelled; }
// Call SetState() with either State_Failed or State_Completed appropriate
// for the response status.
void SetFinalStateFromStatus();
@@ -121,6 +125,9 @@ protected:
static bool IsActiveState(wxWebRequest::State state);
private:
// Called from public Cancel() at most once per object.
virtual void DoCancel() = 0;
wxWebSession& m_session;
wxEvtHandler* const m_handler;
const int m_id;
@@ -128,6 +135,9 @@ private:
wxFileOffset m_bytesReceived;
wxCharBuffer m_dataText;
// Initially false, set to true after the first call to Cancel().
bool m_cancelled;
wxDECLARE_NO_COPY_CLASS(wxWebRequestImpl);
};