Repository: celery/celery

Issue:
MongoDB backend TypeError: unsupported type for timedelta seconds component: NoneType
<!--
Please fill this template entirely and do not erase parts of it.
We reserve the right to close without a response
bug reports which are incomplete.
-->
# Checklist
<!--
To check an item on the list replace [ ] with [x].
-->
- [ ] I have verified that the issue exists against the `master` branch of Celery.
- [ ] This has already been asked to the [discussion group](https://groups.google.com/forum/#!forum/celery-users) first.
- [ x] I have read the relevant section in the
  [contribution guide](http://docs.celeryproject.org/en/latest/contributing.html#other-bugs)
  on reporting bugs.
- [x] I have checked the [issues list](https://github.com/celery/celery/issues?q=is%3Aissue+label%3A%22Issue+Type%3A+Bug+Report%22+-label%3A%22Category%3A+Documentation%22)
  for similar or identical bug reports.
- [x] I have checked the [pull requests list](https://github.com/celery/celery/pulls?q=is%3Apr+label%3A%22PR+Type%3A+Bugfix%22+-label%3A%22Category%3A+Documentation%22)
  for existing proposed fixes.
- [x] I have checked the [commit log](https://github.com/celery/celery/commits/master)
  to find out if the bug was already fixed in the master branch.
- [x] I have included all related issues and possible duplicate issues
  in this issue (If there are none, check this box anyway).

## Mandatory Debugging Information

- [ ] I have included the output of ``celery -A proj report`` in the issue.
    (if you are not able to do this, then at least specify the Celery
     version affected).
- [ ] I have verified that the issue exists against the `master` branch of Celery.
- [x] I have included the contents of ``pip freeze`` in the issue.
- [x] I have included all the versions of all the external dependencies required
  to reproduce this bug.

## Optional Debugging Information
<!--
Try some of the below if you think they are relevant.
It will help us figure out the scope of the bug and how many users it affects.
-->
- [ ] I have tried reproducing the issue on more than one Python version
  and/or implementation.
- [ ] I have tried reproducing the issue on more than one message broker and/or
  result backend.
- [ ] I have tried reproducing the issue on more than one version of the message
  broker and/or result backend.
- [ ] I have tried reproducing the issue on more than one operating system.
- [ ] I have tried reproducing the issue on more than one workers pool.
- [ ] I have tried reproducing the issue with autoscaling, retries,
  ETA/Countdown & rate limits disabled.
- [ ] I have tried reproducing the issue after downgrading
  and/or upgrading Celery and its dependencies.

## Related Issues and Possible Duplicates
<!--
Please make sure to search and mention any related issues
or possible duplicates to this issue as requested by the checklist above.

This may or may not include issues in other repositories that the Celery project
maintains or other repositories that are dependencies of Celery.

If you don't know how to mention issues, please refer to Github's documentation
on the subject: https://help.github.com/en/articles/autolinked-references-and-urls#issues-and-pull-requests
-->

#### Related Issues

- None

#### Possible Duplicates

- None

## Environment & Settings
<!-- Include the contents of celery --version below -->
**Celery version**: 4.4.6 (cliffs)
<!-- Include the output of celery -A proj report below -->
<details>
<summary><b><code>celery report</code> Output:</b></summary>
<p>

```
can't disclose that
```

</p>
</details>

# Steps to Reproduce

## Required Dependencies
<!-- Please fill the required dependencies to reproduce this issue -->
* **Minimal Python Version**: Python 3.8.5
* **Minimal Celery Version**: 4.4.6 (cliffs)
* **Minimal Kombu Version**: 4.6.11
* **Minimal Broker Version**: N/A or Unknown
* **Minimal Result Backend Version**: N/A or Unknown
* **Minimal OS and/or Kernel Version**: N/A or Unknown
* **Minimal Broker Client Version**: N/A or Unknown
* **Minimal Result Backend Client Version**: N/A or Unknown

### Python Packages
<!-- Please fill the contents of pip freeze below -->
<details>
<summary><b><code>pip freeze</code> Output:</b></summary>
<p>

```
alembic==1.4.3
amqp==2.6.1
apache-airflow==1.10.12
apispec==1.3.3
appdirs==1.4.4
argcomplete==1.12.0
asgiref==3.2.10
astroid==2.4.2
attrs==19.3.0
Babel==2.8.0
billiard==3.6.3.0
cached-property==1.5.1
cachetools==4.1.1
cattrs==1.0.0
celery==4.4.6
certifi==2020.6.20
cffi==1.14.2
chardet==3.0.4
click==7.1.2
colorama==0.4.3
colorlog==4.0.2
configparser==3.5.3
coreapi==2.3.3
coreschema==0.0.4
croniter==0.3.34
cryptography==3.1
dataclasses==0.6
defusedxml==0.7.0rc1
dill==0.3.2
distlib==0.3.1
Django==3.0.5
django-braces==1.14.0
django-celery-beat==2.0.0
django-celery-results==1.2.1
django-cors-headers==3.5.0
django-debug-toolbar==2.2
django-extensions==3.0.9
django-oauth-toolkit==1.3.2
django-rest-framework-social-oauth2==1.1.0
django-rest-swagger==2.2.0
django-tenant-schemas==1.10.0
django-timezone-field==4.0
djangorestframework==3.11.1
djangorestframework-csv==2.1.0
djongo==1.3.3
dnspython==1.16.0
docutils==0.16
email-validator==1.1.1
eventlet==0.29.0
filelock==3.0.12
Flask==1.1.2
Flask-Admin==1.5.4
Flask-AppBuilder==2.3.4
Flask-Babel==1.0.0
Flask-Caching==1.3.3
Flask-JWT-Extended==3.24.1
Flask-Login==0.4.1
Flask-OpenID==1.2.5
Flask-SQLAlchemy==2.4.4
flask-swagger==0.2.14
Flask-WTF==0.14.3
funcsigs==1.0.2
future==0.18.2
gevent==20.6.2
google-api-core==1.22.2
google-api-python-client==1.11.0
google-auth==1.21.1
google-auth-httplib2==0.0.4
google-auth-oauthlib==0.4.1
google-cloud-core==1.4.1
google-cloud-logging==1.15.1
google-cloud-tasks==2.0.0
googleapis-common-protos==1.52.0
graphviz==0.14.1
greenlet==0.4.16
grpc-google-iam-v1==0.12.3
grpcio==1.32.0
gunicorn==20.0.4
httplib2==0.18.1
hvac==0.10.5
idna==2.10
iso8601==0.1.13
isort==5.5.2
itsdangerous==1.1.0
itypes==1.2.0
Jinja2==2.11.2
json-merge-patch==0.2
jsonschema==3.2.0
kombu==4.6.11
lazy-object-proxy==1.4.3
libcst==0.3.10
lockfile==0.12.2
Mako==1.1.3
Markdown==2.6.11
MarkupSafe==1.1.1
marshmallow==2.21.0
marshmallow-enum==1.5.1
marshmallow-sqlalchemy==0.23.1
mccabe==0.6.1
monotonic==1.5
mypy-extensions==0.4.3
mysqlclient==2.0.1
natsort==7.0.1
numpy==1.19.2
oauthlib==3.1.0
openapi-codec==1.3.2
ordered-set==4.0.2
pandas==1.1.2
pbr==5.5.0
pendulum==1.4.4
prison==0.1.3
proto-plus==1.9.1
protobuf==3.13.0
psutil==5.7.2
psycopg2==2.8.6
psycopg2-binary==2.8.6
pyasn1==0.4.8
pyasn1-modules==0.2.8
pycparser==2.20
pydot==1.4.1
Pygments==2.7.1
pygraphviz==1.6
PyJWT==1.7.1
pylint==2.6.0
pymongo==3.11.0
pyOpenSSL==19.1.0
pyparsing==2.4.7
pyrsistent==0.17.3
python-crontab==2.5.1
python-daemon==2.2.4
python-dateutil==2.8.1
python-editor==1.0.4
python-nvd3==0.15.0
python-slugify==4.0.1
python3-openid==3.2.0
pytz==2020.1
pytzdata==2020.1
PyYAML==5.3.1
redis==3.5.3
requests==2.24.0
requests-oauthlib==1.3.0
rsa==4.6
scout-apm==2.16.1
sentry-sdk==0.17.4
setproctitle==1.1.10
simplejson==3.17.2
six==1.15.0
social-auth-app-django==3.4.0
social-auth-core==3.3.3
SQLAlchemy==1.3.19
SQLAlchemy-JSONField==0.9.0
SQLAlchemy-Utils==0.36.8
sqlparse==0.2.4
stevedore==3.2.1
tabulate==0.8.7
tenacity==4.12.0
text-unidecode==1.3
thrift==0.13.0
toml==0.10.1
typing-extensions==3.7.4.3
typing-inspect==0.6.0
tzlocal==1.5.1
unicodecsv==0.14.1
uritemplate==3.0.1
urllib3==1.25.10
vine==1.3.0
virtualenv==20.0.31
virtualenv-clone==0.5.4
virtualenvwrapper==4.8.4
Werkzeug==0.16.1
wrapt==1.12.1
WTForms==2.3.3
zope.deprecation==4.4.0
zope.event==4.4
zope.interface==5.1.0
```

</p>
</details>

### Other Dependencies
<!--
Please provide system dependencies, configuration files
and other dependency information if applicable
-->
<details>
<p>
N/A
</p>
</details>

## Minimally Reproducible Test Case
<!--
Please provide a reproducible test case.
Refer to the Reporting Bugs section in our contribution guide.

We prefer submitting test cases in the form of a PR to our integration test suite.
If you can provide one, please mention the PR number below.
If not, please attach the most minimal code example required to reproduce the issue below.
If the test case is too large, please include a link to a gist or a repository below.
-->

<details>
<p>

```python
```

</p>
</details>

# Expected Behavior
<!-- Describe in detail what you expect to happen -->

Celery task is executed successfully 

# Actual Behavior
<!--
Describe in detail what actually happened.
Please include a backtrace and surround it with triple backticks (```).
In addition, include the Celery daemon logs, the broker logs,
the result backend logs and system logs below if they will help us debug
the issue.
-->

Fails with TypeError:
```
Task celery.backend_cleanup[75aa0973-534f-4812-a992-1ead4086f58c] raised unexpected: TypeError('unsupported type for timedelta seconds component: NoneType')
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/kombu/utils/objects.py", line 42, in __get__
    return obj.__dict__[self.__name__]
KeyError: 'expires_delta'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 412, in trace_task
    R = retval = fun(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/celery/app/trace.py", line 704, in __protected_call__
    return self.run(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/celery/app/builtins.py", line 25, in backend_cleanup
    app.backend.cleanup()
  File "/usr/local/lib/python3.9/site-packages/celery/backends/mongodb.py", line 258, in cleanup
    {'date_done': {'$lt': self.app.now() - self.expires_delta}},
  File "/usr/local/lib/python3.9/site-packages/kombu/utils/objects.py", line 44, in __get__
    value = obj.__dict__[self.__name__] = self.__get(obj)
  File "/usr/local/lib/python3.9/site-packages/celery/backends/mongodb.py", line 312, in expires_delta
    return timedelta(seconds=self.expires)
TypeError: unsupported type for timedelta seconds component: NoneType
```


Discussion:
Please provide a reproducible test case.
I'll reopen this issue once you do.
When expires is None it means that no result expires right?
I have a fix anyway.
Yes, that makes sense to me.

Resolving patch:
diff --git a/celery/backends/mongodb.py b/celery/backends/mongodb.py
--- a/celery/backends/mongodb.py
+++ b/celery/backends/mongodb.py
@@ -248,6 +248,9 @@ def _forget(self, task_id):
 
     def cleanup(self):
         """Delete expired meta-data."""
+        if not self.expires:
+            return
+
         self.collection.delete_many(
             {'date_done': {'$lt': self.app.now() - self.expires_delta}},
         )


Test patch:
diff --git a/t/unit/backends/test_mongodb.py b/t/unit/backends/test_mongodb.py
--- a/t/unit/backends/test_mongodb.py
+++ b/t/unit/backends/test_mongodb.py
@@ -485,6 +485,12 @@ def test_cleanup(self, mock_get_database):
         mock_get_database.assert_called_once_with()
         mock_collection.delete_many.assert_called()
 
+        self.backend.collections = mock_collection = Mock()
+        self.backend.expires = None
+
+        self.backend.cleanup()
+        mock_collection.delete_many.assert_not_called()
+
     def test_get_database_authfailure(self):
         x = MongoBackend(app=self.app)
         x._get_connection = Mock()