Skip to content

Commit

Permalink
http: Add a safety bailout on list load.
Browse files Browse the repository at this point in the history
Hopefully this will prevent a crash.  Currently http::Client uses blocking
reads so it will just hang.
  • Loading branch information
unknownbrackets committed Jul 4, 2016
1 parent 287d196 commit f53735f
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions UI/RemoteISOScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ static ServerStatus serverStatus;
static recursive_mutex serverStatusLock;
static condition_variable serverStatusCond;

static bool scanCancelled = false;

static void UpdateStatus(ServerStatus s) {
lock_guard guard(serverStatusLock);
serverStatus = s;
Expand Down Expand Up @@ -194,7 +196,7 @@ static bool FindServer(std::string &resultHost, int &resultPort) {
http.Disconnect();
}

if (code != 200) {
if (code != 200 || scanCancelled) {
return false;
}

Expand Down Expand Up @@ -247,7 +249,7 @@ static bool LoadGameList(const std::string &host, int port, std::vector<std::str
http.Disconnect();
}

if (code != 200) {
if (code != 200 || scanCancelled) {
return false;
}

Expand Down Expand Up @@ -365,6 +367,7 @@ UI::EventReturn RemoteISOScreen::HandleBrowse(UI::EventParams &e) {
}

RemoteISOConnectScreen::RemoteISOConnectScreen() : status_(ScanStatus::SCANNING), nextRetry_(0.0) {
scanCancelled = false;
statusLock_ = new recursive_mutex();

scanThread_ = new std::thread([](RemoteISOConnectScreen *thiz) {
Expand All @@ -374,8 +377,14 @@ RemoteISOConnectScreen::RemoteISOConnectScreen() : status_(ScanStatus::SCANNING)
}

RemoteISOConnectScreen::~RemoteISOConnectScreen() {
int maxWait = 5000;
scanCancelled = true;
while (GetStatus() == ScanStatus::SCANNING || GetStatus() == ScanStatus::LOADING) {
sleep_ms(1);
if (--maxWait < 0) {
// If it does ever wake up, it may crash... but better than hanging?
break;
}
}
delete scanThread_;
delete statusLock_;
Expand Down Expand Up @@ -456,6 +465,9 @@ void RemoteISOConnectScreen::update(InputState &input) {

void RemoteISOConnectScreen::ExecuteScan() {
FindServer(host_, port_);
if (scanCancelled) {
return;
}

lock_guard guard(*statusLock_);
status_ = host_.empty() ? ScanStatus::FAILED : ScanStatus::FOUND;
Expand All @@ -468,6 +480,9 @@ ScanStatus RemoteISOConnectScreen::GetStatus() {

void RemoteISOConnectScreen::ExecuteLoad() {
bool result = LoadGameList(host_, port_, games_);
if (scanCancelled) {
return;
}

lock_guard guard(*statusLock_);
status_ = result ? ScanStatus::LOADED : ScanStatus::FAILED;
Expand Down

0 comments on commit f53735f

Please sign in to comment.