static int last_form_empty;
 
 static int sig_cmd_fd;
+static struct openconnect_info *sig_vpninfo;
 
 static void add_form_field(char *field);
 
        if (write(sig_cmd_fd, &cmd, 1) < 0) {
        /* suppress warn_unused_result */
        }
+       if (sig_vpninfo)
+               sig_vpninfo->need_poll_cmd_fd = 1;
 }
 #else /* _WIN32 */
 static const char *default_vpncscript;
                if (!fp) {
                        fprintf(stderr, _("Failed to open '%s' for write: %s\n"),
                                pidfile, strerror(errno));
+                       sig_vpninfo = NULL;
                        openconnect_vpninfo_free(vpninfo);
                        exit(1);
                }
                vpn_progress(vpninfo, PRG_INFO,
                             _("Continuing in background; pid %d\n"),
                             pid);
+               sig_vpninfo = NULL;
                openconnect_vpninfo_free(vpninfo);
                exit(0);
        }
        sigaction(SIGUSR2, &sa, NULL);
 #endif /* !_WIN32 */
 
+       sig_vpninfo = vpninfo;
        sig_cmd_fd = openconnect_setup_cmd_pipe(vpninfo);
        if (sig_cmd_fd < 0) {
                fprintf(stderr, _("Error opening cmd pipe\n"));
                exit(1);
        }
+       vpninfo->cmd_fd_internal = 1;
 
        if (vpninfo->certinfo[0].key && do_passphrase_from_fsid)
                openconnect_passphrase_from_fsid(vpninfo);
                        printf("RESOLVE='%s:%.*s'\n", vpninfo->hostname, l, p);
                } else
                        printf("RESOLVE=");
+               sig_vpninfo = NULL;
                openconnect_vpninfo_free(vpninfo);
                exit(0);
        } else if (cookieonly) {
                printf("%s\n", vpninfo->cookie);
                if (cookieonly == 1) {
                        /* We use cookieonly=2 for 'print it and continue' */
+                       sig_vpninfo = NULL;
                        openconnect_vpninfo_free(vpninfo);
                        exit(0);
                }
                break;
        }
 
+       sig_vpninfo = NULL;
        openconnect_vpninfo_free(vpninfo);
        exit(ret);
 }
 
                if (vpninfo->quit_reason)
                        break;
 
-               poll_cmd_fd(vpninfo, 0);
+               if (vpninfo->need_poll_cmd_fd)
+                       poll_cmd_fd(vpninfo, 0);
+
                if (vpninfo->got_cancel_cmd) {
                        if (vpninfo->delay_close != NO_DELAY_CLOSE) {
                                if (vpninfo->delay_close == DELAY_CLOSE_IMMEDIATE_CALLBACK) {
 
                tv.tv_sec = now >= expiration ? 0 : expiration - now;
                tv.tv_usec = 0;
 
+               /* If the cmd_fd is internal and we've been told to poll it,
+                * don't *keep* doing so afterwards. */
+               vpninfo->need_poll_cmd_fd = !vpninfo->cmd_fd_internal;
+
                FD_ZERO(&rd_set);
                cmd_fd_set(vpninfo, &rd_set, &maxfd);
                if (select(maxfd + 1, &rd_set, NULL, NULL, &tv) < 0 &&
                    errno != EINTR) {
                        vpn_perror(vpninfo, _("Failed select() for command socket"));
                }
-
-               check_cmd_fd(vpninfo, &rd_set);
+               if (FD_ISSET(vpninfo->cmd_fd, &rd_set)) {
+                       vpninfo->need_poll_cmd_fd = 1; /* Until it's *empty */
+                       check_cmd_fd(vpninfo, &rd_set);
+               }
        }
 }