Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] InterfaceError while consuming document
### Description

Since Version 1.80 all kind of documents (docx, PDF, etc) wich i put in my consume folder executing the consuming process stops with the following error:

### Steps to reproduce

Insert a document into the consume folder.

### Webserver logs

```bash
[2022-08-03 21:10:54,601] [INFO] [paperless.management.consumer] Adding /usr/src/paperless/consume/TEST.docx to the task queue.
[2022-08-03 21:10:54,604] [ERROR] [paperless.management.consumer] Error while consuming document
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 262, in _cursor
    return self._prepare_cursor(self.create_cursor(name))
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 256, in create_cursor
    cursor = self.connection.cursor()
psycopg2.InterfaceError: connection already closed

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/paperless/src/documents/management/commands/document_consumer.py", line 94, in _consume
    async_task(
  File "/usr/src/paperless/src/docker/src/django-q/django_q/tasks.py", line 67, in async_task
    pre_enqueue.send(sender="django_q", task=task)
  File "/usr/local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 176, in send
    return [
  File "/usr/local/lib/python3.9/site-packages/django/dispatch/dispatcher.py", line 177, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "/usr/src/paperless/src/documents/signals/handlers.py", line 511, in init_paperless_task
    paperless_task, created = PaperlessTask.objects.get_or_create(
  File "/usr/local/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 657, in get_or_create
    return self.get(**kwargs), False
  File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 492, in get
    num = len(clone)
  File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 302, in __len__
    self._fetch_all()
  File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 1507, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 57, in __iter__
    results = compiler.execute_sql(
  File "/usr/local/lib/python3.9/site-packages/django/db/models/sql/compiler.py", line 1359, in execute_sql
    cursor = self.connection.cursor()
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 284, in cursor
    return self._cursor()
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 262, in _cursor
    return self._prepare_cursor(self.create_cursor(name))
  File "/usr/local/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/base/base.py", line 262, in _cursor
    return self._prepare_cursor(self.create_cursor(name))
  File "/usr/local/lib/python3.9/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 256, in create_cursor
    cursor = self.connection.cursor()
django.db.utils.InterfaceError: connection already closed
```


### Paperless-ngx version

1.80

### Host OS

Synology NAS

### Installation method

Docker - official image

### Browser

_No response_

### Configuration changes

no changes since 1.7.0

### Other

_No response_


Discussion:
Thank you for the bug report.  This new task viewing functionality is being a real pain.  I'm not sure what the solution will be here yet.

Resolving patch:
diff --git a/src/documents/signals/handlers.py b/src/documents/signals/handlers.py
--- a/src/documents/signals/handlers.py
+++ b/src/documents/signals/handlers.py
@@ -11,7 +11,6 @@
 from django.db import DatabaseError
 from django.db import models
 from django.db.models import Q
-from django.db.utils import OperationalError
 from django.dispatch import receiver
 from django.utils import termcolors
 from django.utils import timezone
@@ -514,7 +513,9 @@ def init_paperless_task(sender, task, **kwargs):
             paperless_task.name = task["name"]
             paperless_task.created = task["started"]
             paperless_task.save()
-        except OperationalError as e:
+        except Exception as e:
+            # Don't let an exception in the signal handlers prevent
+            # a document from being consumed.
             logger.error(f"Creating PaperlessTask failed: {e}")
 
 
@@ -527,10 +528,10 @@ def paperless_task_started(sender, task, **kwargs):
             )
             paperless_task.started = timezone.now()
             paperless_task.save()
-    except OperationalError as e:
-        logger.error(f"Creating PaperlessTask failed: {e}")
     except PaperlessTask.DoesNotExist:
         pass
+    except Exception as e:
+        logger.error(f"Creating PaperlessTask failed: {e}")
 
 
 @receiver(models.signals.post_save, sender=django_q.models.Task)
@@ -542,7 +543,7 @@ def update_paperless_task(sender, instance, **kwargs):
             )
             paperless_task.attempted_task = instance
             paperless_task.save()
-    except OperationalError as e:
-        logger.error(f"Creating PaperlessTask failed: {e}")
     except PaperlessTask.DoesNotExist:
         pass
+    except Exception as e:
+        logger.error(f"Creating PaperlessTask failed: {e}")


Test patch: