-- Dmsg2(800, "set_jcr_job_status(%s, %c)\n", jcr->Job, JobStatus);
-+ Dmsg2(2, "set_jcr_job_status(%s, %c)\n", jcr->Job, JobStatus);
- /* if wait state is new, we keep current time for watchdog MaxWaitTime */
- switch (JobStatus) {
- case JS_WaitFD:
-@@ -740,7 +765,7 @@
- * For a set of errors, ... keep the current status
- * so it isn't lost. For all others, set it.
- */
-- Dmsg3(300, "jid=%u OnEntry JobStatus=%c set=%c\n", (uint32_t)jcr->JobId,
-+ Dmsg3(2, "jid=%u OnEntry JobStatus=%c set=%c\n", (uint32_t)jcr->JobId,
- jcr->JobStatus, JobStatus);
- switch (jcr->JobStatus) {
- case JS_ErrorTerminated:
-@@ -781,7 +806,7 @@
+ if (fd) {
+- if (timeout) {
+- tid = start_bsock_timer(fd, timeout); /* TODO: New timeout directive??? */
+- }
+- /* Wait for Client to terminate */
+- while ((n = bget_dirmsg(fd)) >= 0) {
+- if (!fd_ok &&
+- (sscanf(fd->msg, EndJob, &jcr->FDJobStatus, &JobFiles,
+- &ReadBytes, &JobBytes, &Errors, &VSS, &Encrypt) == 7 ||
+- sscanf(fd->msg, OldEndJob, &jcr->FDJobStatus, &JobFiles,
+- &ReadBytes, &JobBytes, &Errors) == 5)) {
+- fd_ok = true;
+- set_jcr_job_status(jcr, jcr->FDJobStatus);
+- Dmsg1(100, "FDStatus=%c\n", (char)jcr->JobStatus);
+- } else {
+- Jmsg(jcr, M_WARNING, 0, _("Unexpected Client Job message: %s\n"),
+- fd->msg);
++ /* Wait for Client to terminate
++ * In some conditions, the client isn't able to send
++ * any messages and we should not wait for ages
++ */
++ int OK=true;
++ int ret;
++ while (OK && expect_EndJob) {
++
++ /* Even if the job is canceled, we let a chance to FD to
++ * send EndJob message
++ */
++ if (job_canceled(jcr)) {
++ OK=false;
+ }
+- if (job_canceled(jcr)) {
+- break;
++
++ /* wait for data few minutes */
++ ret = fd->wait_data_intr(3*60, 0);
++ if (ret == 1) { /* get data */
++ n = bget_dirmsg(fd);
++ if (n >= 0 &&
++ (sscanf(fd->msg, EndJob, &jcr->FDJobStatus, &JobFiles,
++ &ReadBytes, &JobBytes, &Errors, &VSS, &Encrypt) == 7 ||
++ sscanf(fd->msg, OldEndJob, &jcr->FDJobStatus, &JobFiles,
++ &ReadBytes, &JobBytes, &Errors) == 5)) {
++ fd_ok = true;
++ set_jcr_job_status(jcr, jcr->FDJobStatus);
++ OK=false; /* end of loop */
++ } else {
++ Jmsg(jcr, M_WARNING, 0, _("Unexpected Client Job message: %s\n"),
++ fd->msg);
++ }
++ } /* else get timeout or network error */
++
++ if (is_bnet_error(fd)) {
++ Jmsg(jcr, M_FATAL, 0, _("Network error with FD during %s: ERR=%s\n"),
++ job_type_to_str(jcr->get_JobType()), fd->bstrerror());
++ OK=false;
+ }