Skip to content

Commit

Permalink
race condition fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chapati committed Aug 10, 2021
1 parent 20b4eb8 commit 303f9c5
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/src/qiodevicecopier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@

#include <QIODevice>
#include <QTimer>

#include <QPointer>
#include <qhttpengine/qiodevicecopier.h>

#include "qiodevicecopier_p.h"

using namespace QHttpEngine;
Expand Down Expand Up @@ -94,7 +93,12 @@ void QIODeviceCopierPrivate::nextBlock()
if (src->atEnd() || (rangeTo != -1 && src->pos() > rangeTo)) {
Q_EMIT q->finished();
} else {
QTimer::singleShot(0, this, &QIODeviceCopierPrivate::nextBlock);
QPointer<QIODeviceCopierPrivate> qp = this;
QTimer::singleShot(0, this, [qp]() {
if (qp) {
qp->nextBlock();
}
});
}
}

Expand Down Expand Up @@ -154,9 +158,20 @@ void QIODeviceCopier::start()
connect(d->src, &QIODevice::readChannelFinished, d, &QIODeviceCopierPrivate::onReadChannelFinished);

// The first read from the device needs to be triggered
QTimer::singleShot(0, d, d->src->isSequential() ?
&QIODeviceCopierPrivate::onReadyRead :
&QIODeviceCopierPrivate::nextBlock);
QPointer<QIODeviceCopierPrivate> qp = d;
bool isSeq = d->src->isSequential();
QTimer::singleShot(0, d, [qp, isSeq]() {
if (qp) {
if (isSeq)
{
qp->onReadyRead();
}
else
{
qp->nextBlock();
}
}
});
}

void QIODeviceCopier::stop()
Expand Down

0 comments on commit 303f9c5

Please sign in to comment.