Skip to content

Commit ff87f48

Browse files
committed
Fix window close before wait scenario
1 parent fafbea3 commit ff87f48

1 file changed

Lines changed: 28 additions & 15 deletions

File tree

src/webui.c

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,9 @@ typedef struct _webui_core_t {
468468
char* ssl_cert;
469469
char* ssl_key;
470470
#endif
471-
// WebView
471+
bool is_main_run;
472472
bool is_browser_main_run;
473+
// WebView
473474
bool is_webview;
474475
#ifdef _WIN32
475476
char* webview_cacheFolder;
@@ -1358,6 +1359,7 @@ void webui_close(size_t window) {
13581359

13591360
// Close
13601361
if (!win->webView) {
1362+
// Close web browser window
13611363
if (_webui_mutex_is_connected(win, WEBUI_MUTEX_GET_STATUS)) {
13621364
// Packet Protocol Format:
13631365
// [...]
@@ -3585,6 +3587,13 @@ void webui_wait(void) {
35853587
if (_webui_mutex_app_is_exit_now(WEBUI_MUTEX_GET_STATUS))
35863588
return;
35873589

3590+
if (_webui.is_main_run) {
3591+
#ifdef WEBUI_LOG
3592+
_webui_log_debug("[Loop] webui_wait() -> Already in main loop. Stop.\n");
3593+
#endif
3594+
return;
3595+
}
3596+
35883597
if (_webui.startup_timeout > 0) {
35893598

35903599
// Check if there is atleast one window (UI)
@@ -3618,7 +3627,11 @@ void webui_wait(void) {
36183627
// called by the user.
36193628
}
36203629

3630+
// Lock mutex
3631+
_webui_mutex_lock(&_webui.mutex_wait);
3632+
36213633
// Main loop
3634+
_webui.is_main_run = true;
36223635
#ifdef _WIN32
36233636
if (!_webui.is_webview) {
36243637
// Windows Web browser main loop
@@ -3628,7 +3641,6 @@ void webui_wait(void) {
36283641
#endif
36293642

36303643
_webui.is_browser_main_run = true;
3631-
_webui_mutex_lock(&_webui.mutex_wait);
36323644
_webui_condition_wait(&_webui.condition_wait, &_webui.mutex_wait);
36333645
_webui.is_browser_main_run = false;
36343646
}
@@ -3639,7 +3651,6 @@ void webui_wait(void) {
36393651
_webui_log_debug("[Loop] webui_wait() -> Windows WebView loop\n");
36403652
#endif
36413653

3642-
_webui_mutex_lock(&_webui.mutex_wait);
36433654
_webui_condition_wait(&_webui.condition_wait, &_webui.mutex_wait);
36443655
}
36453656
#elif __linux__
@@ -3651,7 +3662,6 @@ void webui_wait(void) {
36513662
#endif
36523663

36533664
_webui.is_browser_main_run = true;
3654-
_webui_mutex_lock(&_webui.mutex_wait);
36553665
_webui_condition_wait(&_webui.condition_wait, &_webui.mutex_wait);
36563666
_webui.is_browser_main_run = false;
36573667
}
@@ -3678,7 +3688,6 @@ void webui_wait(void) {
36783688
#endif
36793689

36803690
_webui.is_browser_main_run = true;
3681-
_webui_mutex_lock(&_webui.mutex_wait);
36823691
_webui_condition_wait(&_webui.condition_wait, &_webui.mutex_wait);
36833692
_webui.is_browser_main_run = false;
36843693
}
@@ -3697,6 +3706,7 @@ void webui_wait(void) {
36973706
_webui.is_wkwebview_main_run = false;
36983707
}
36993708
#endif
3709+
_webui.is_main_run = false;
37003710

37013711
#ifdef WEBUI_LOG
37023712
_webui_log_debug("[Loop] webui_wait() -> Cleaning\n");
@@ -10064,16 +10074,19 @@ static WEBUI_THREAD_SERVER_START {
1006410074
mg_stop(http_ctx);
1006510075

1006610076
// Fire the mutex condition for wait()
10067-
if (_webui.startup_timeout > 0 && _webui.servers < 1) {
10068-
10069-
// Stop all threads
10070-
_webui.ui = false;
10071-
_webui_mutex_app_is_exit_now(WEBUI_MUTEX_SET_TRUE);
10072-
// Break main loop
10073-
_webui_condition_signal(&_webui.condition_wait);
10074-
#ifdef __APPLE__
10075-
_webui_macos_wv_stop();
10076-
#endif
10077+
if ((_webui.startup_timeout > 0) && (_webui.servers < 1)) {
10078+
// Exit app only if user called `webui_wait()` and
10079+
// now all window's threads and servers are closed.
10080+
if (_webui.is_main_run) {
10081+
// Stop all threads
10082+
_webui.ui = false;
10083+
_webui_mutex_app_is_exit_now(WEBUI_MUTEX_SET_TRUE);
10084+
// Break main loop
10085+
_webui_condition_signal(&_webui.condition_wait);
10086+
#ifdef __APPLE__
10087+
_webui_macos_wv_stop();
10088+
#endif
10089+
}
1007710090
}
1007810091

1007910092
// Clean monitor thread

0 commit comments

Comments
 (0)