diff --git a/.travis.yml b/.travis.yml
index e093a8d7..f39bb765 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -84,8 +84,8 @@ script:
     # run it with nightly cpython. Coverage is very slow on pypy.
     - if [[ $TRAVIS_PYTHON_VERSION != nightly && $TRAVIS_PYTHON_VERSION != 'pypy'* ]]; then export RUN_COVERAGE=1; fi
     - if [[ "$RUN_COVERAGE" == 1 ]]; then export TARGET="-m coverage run $TARGET"; fi
-    # See comments in tox.ini
-    - export PYTHONWARNINGS=error,ignore:::site
+    # See comments in tox.ini. Disabled on py35 because ResourceWarnings are too noisy there.
+    - if [[ $TRAVIS_PYTHON_VERSION != '3.5' ]]; then export PYTHONWARNINGS=error,ignore:::site; fi
     - python -bb $TARGET
     - python -O $TARGET
     - LANG=C python $TARGET
diff --git a/tornado/httpclient.py b/tornado/httpclient.py
index d1c92a49..b6344cff 100644
--- a/tornado/httpclient.py
+++ b/tornado/httpclient.py
@@ -233,9 +233,14 @@ class AsyncHTTPClient(Configurable):
             return
         self._closed = True
         if self._instance_cache is not None:
-            if self._instance_cache.get(self.io_loop) is not self:
+            cached_val = self._instance_cache.pop(self.io_loop, None)
+            # If there's an object other than self in the instance
+            # cache for our IOLoop, something has gotten mixed up. A
+            # value of None appears to be possible when this is called
+            # from a destructor (HTTPClient.__del__) as the weakref
+            # gets cleared before the destructor runs.
+            if cached_val is not None and cached_val is not self:
                 raise RuntimeError("inconsistent AsyncHTTPClient cache")
-            del self._instance_cache[self.io_loop]
 
     def fetch(
         self,

RuntimeError: inconsistent AsyncHTTPClient cache
Tornado version: 5.1.1

Running the following code causes `RuntimeError: inconsistent AsyncHTTPClient cache`

```python
from tornado import httpclient


def f():
    pass


c = httpclient.HTTPClient()
```

Error log:

```
Exception ignored in: <bound method HTTPClient.__del__ of <tornado.httpclient.HTTPClient object at 0x7fef4def84a8>>
Traceback (most recent call last):
  File "/████/lib/python3.5/site-packages/tornado/httpclient.py", line 99, in __del__
  File "/████/lib/python3.5/site-packages/tornado/httpclient.py", line 104, in close
  File "/████/lib/python3.5/site-packages/tornado/simple_httpclient.py", line 139, in close
  File "/████/lib/python3.5/site-packages/tornado/httpclient.py", line 220, in close
RuntimeError: inconsistent AsyncHTTPClient cache
```

Since the error is ignored, it may be harmless to the program. But the error message may confuse some new users, so I think it should be fixed.
diff --git a/tornado/test/httpclient_test.py b/tornado/test/httpclient_test.py
index 0a732ddc..4677101c 100644
--- a/tornado/test/httpclient_test.py
+++ b/tornado/test/httpclient_test.py
@@ -6,12 +6,14 @@ import copy
 import threading
 import datetime
 from io import BytesIO
+import subprocess
+import sys
 import time
 import typing  # noqa: F401
 import unicodedata
 import unittest
 
-from tornado.escape import utf8, native_str
+from tornado.escape import utf8, native_str, to_unicode
 from tornado import gen
 from tornado.httpclient import (
     HTTPRequest,
@@ -676,6 +678,35 @@ class SyncHTTPClientTest(unittest.TestCase):
         self.assertEqual(assertion.exception.code, 404)
 
 
+class SyncHTTPClientSubprocessTest(unittest.TestCase):
+    def test_destructor_log(self):
+        # Regression test for
+        # https://github.com/tornadoweb/tornado/issues/2539
+        #
+        # In the past, the following program would log an
+        # "inconsistent AsyncHTTPClient cache" error from a destructor
+        # when the process is shutting down. The shutdown process is
+        # subtle and I don't fully understand it; the failure does not
+        # manifest if that lambda isn't there or is a simpler object
+        # like an int (nor does it manifest in the tornado test suite
+        # as a whole, which is why we use this subprocess).
+        proc = subprocess.run(
+            [
+                sys.executable,
+                "-c",
+                "from tornado.httpclient import HTTPClient; f = lambda: None; c = HTTPClient()",
+            ],
+            stdout=subprocess.PIPE,
+            stderr=subprocess.STDOUT,
+            check=True,
+        )
+        if proc.stdout:
+            print("STDOUT:")
+            print(to_unicode(proc.stdout))
+        if proc.stdout:
+            self.fail("subprocess produced unexpected output")
+
+
 class HTTPRequestTestCase(unittest.TestCase):
     def test_headers(self):
         request = HTTPRequest("http://example.com", headers={"foo": "bar"})

pip install -e .
parse_log_pytest
pytest
flake8 mypy black pytest coverage
apt-get update apt-get install -y gcc
3.6
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs @ file:///opt/conda/conda-bld/attrs_1642510447205/work
black==22.8.0
certifi==2021.5.30
click==8.0.4
coverage==6.2
dataclasses==0.8
flake8==5.0.4
importlib-metadata==4.2.0
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
mccabe==0.7.0
more-itertools @ file:///tmp/build/80754af9/more-itertools_1637733554872/work
mypy==0.971
mypy-extensions==1.0.0
packaging @ file:///tmp/build/80754af9/packaging_1637314298585/work
pathspec==0.9.0
platformdirs==2.4.0
pluggy @ file:///tmp/build/80754af9/pluggy_1615976315926/work
py @ file:///opt/conda/conda-bld/py_1644396412707/work
pycodestyle==2.9.1
pyflakes==2.5.0
pyparsing @ file:///tmp/build/80754af9/pyparsing_1635766073266/work
pytest==6.2.4
toml @ file:///tmp/build/80754af9/toml_1616166611790/work
tomli==1.2.3
-e git+https://github.com/tornadoweb/tornado.git@940fd87fe9145d1154c8457221f86d56ea063c65#egg=tornado
typed-ast==1.5.5
typing_extensions @ file:///opt/conda/conda-bld/typing_extensions_1647553014482/work
zipp @ file:///tmp/build/80754af9/zipp_1633618647012/work

name: tornado
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=21.4.0=pyhd3eb1b0_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - importlib_metadata=4.8.1=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - more-itertools=8.12.0=pyhd3eb1b0_0
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=21.3=pyhd3eb1b0_0
  - pip=21.2.2=py36h06a4308_0
  - pluggy=0.13.1=py36h06a4308_0
  - py=1.11.0=pyhd3eb1b0_0
  - pyparsing=3.0.4=pyhd3eb1b0_0
  - pytest=6.2.4=py36h06a4308_2
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - toml=0.10.2=pyhd3eb1b0_0
  - typing_extensions=4.1.1=pyh06a4308_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.6.0=pyhd3eb1b0_0
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - black==22.8.0
      - click==8.0.4
      - coverage==6.2
      - dataclasses==0.8
      - flake8==5.0.4
      - importlib-metadata==4.2.0
      - mccabe==0.7.0
      - mypy==0.971
      - mypy-extensions==1.0.0
      - pathspec==0.9.0
      - platformdirs==2.4.0
      - pycodestyle==2.9.1
      - pyflakes==2.5.0
      - tomli==1.2.3
      - typed-ast==1.5.5
prefix: /opt/conda/envs/tornado

tornado/test/httpclient_test.py::SyncHTTPClientSubprocessTest::test_destructor_log
tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_304_with_content_length tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_all_methods tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_basic_auth tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_basic_auth_explicit_mode tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_basic_auth_unicode tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_bind_source_ip tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_body_encoding tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_body_sanity_checks tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_chunked tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_chunked_close tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_configure_defaults tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_credentials_in_url tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_follow_redirect tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_future_http_error tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_future_http_error_no_raise tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_future_interface tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_header_callback tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_header_types tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_hello_world tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_multi_line_headers tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_non_ascii_header tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_patch_receives_payload tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_post tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_put_307 tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_response_times tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_reuse_request_from_response tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_streaming_callback tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_types tornado/test/httpclient_test.py::HTTPClientCommonTestCase::test_unsupported_auth_mode tornado/test/httpclient_test.py::RequestProxyTest::test_bad_attribute tornado/test/httpclient_test.py::RequestProxyTest::test_both_set tornado/test/httpclient_test.py::RequestProxyTest::test_default_set tornado/test/httpclient_test.py::RequestProxyTest::test_defaults_none tornado/test/httpclient_test.py::RequestProxyTest::test_neither_set tornado/test/httpclient_test.py::RequestProxyTest::test_request_set tornado/test/httpclient_test.py::HTTPResponseTestCase::test_str tornado/test/httpclient_test.py::SyncHTTPClientTest::test_sync_client tornado/test/httpclient_test.py::SyncHTTPClientTest::test_sync_client_error tornado/test/httpclient_test.py::HTTPRequestTestCase::test_body tornado/test/httpclient_test.py::HTTPRequestTestCase::test_body_setter tornado/test/httpclient_test.py::HTTPRequestTestCase::test_headers tornado/test/httpclient_test.py::HTTPRequestTestCase::test_headers_setter tornado/test/httpclient_test.py::HTTPRequestTestCase::test_if_modified_since tornado/test/httpclient_test.py::HTTPRequestTestCase::test_null_headers_setter tornado/test/httpclient_test.py::HTTPErrorTestCase::test_copy tornado/test/httpclient_test.py::HTTPErrorTestCase::test_error_with_response tornado/test/httpclient_test.py::HTTPErrorTestCase::test_plain_error
.travis.yml tornado/httpclient.py
.travis.yml tornado/httpclient.py