diff --git a/tornado/web.py b/tornado/web.py
index 5fa3abd8..7bf5415d 100644
--- a/tornado/web.py
+++ b/tornado/web.py
@@ -2499,8 +2499,9 @@ class StaticFileHandler(RequestHandler):
 
         .. versionadded:: 3.1
         """
-        if self.check_etag_header():
-            return True
+        # If client sent If-None-Match, use it, ignore If-Modified-Since
+        if self.request.headers.get('If-None-Match'):
+            return self.check_etag_header()
 
         # Check the If-Modified-Since, and don't send the result if the
         # content has not been modified

StaticFileHandler returns 304 despite etag mismatch
I am not sure if this is an actual issue in the code or with my deployment, but it caused me some head-scratching.

I am using Tornado as an API webserver for an embedded system. It also serves a small web application using the StaticFileHandler. To fix issues with caching browsers, I subclassed the StaticFileHandler to send Cache-Control headers like so:

    class NoCacheStaticFileHandler(tornado.web.StaticFileHandler):
        def set_extra_headers(self, path):
            self.set_header('Cache-control', 'no-cache, must-revalidate, max-age=0')

The problem I am seeing is that, despite the browser sending a different Etag than the server calculates, the server still sends a 304. I dug a bit into the code and found the following:

    def should_return_304(self):
        """Returns True if the headers indicate that we should return 304.

        .. versionadded:: 3.1
        """
        if self.check_etag_header():
            return True

        # Check the If-Modified-Since, and don't send the result if the
        # content has not been modified
        ims_value = self.request.headers.get("If-Modified-Since")
        if ims_value is not None:
            date_tuple = email.utils.parsedate(ims_value)
            if date_tuple is not None:
                if_since = datetime.datetime(*date_tuple[:6])
                if if_since >= self.modified:
                    return True

        return False

Essentially, this seems to return 304 if either Etag headers match or the If-Modified-Since date the client sent is later than the file on the server. Is this how it should be? The browser sends a different hash, the server knows that the versions do not match but still sends a 304 because the date is newer? It feels like the server should not send a 304 when one property does not match.

The issue might be specific to the system here since the dates there are not very reliable. But I had it several times now that the data on the server was updated but it kept sending 304 despite an Etag mismatch because of the If-Modified-Since headers.
diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py
index 646b2f79..39347deb 100644
--- a/tornado/test/web_test.py
+++ b/tornado/test/web_test.py
@@ -1081,6 +1081,13 @@ class StaticFileTest(WebTestCase):
             'If-None-Match': response1.headers['Etag']})
         self.assertEqual(response2.code, 304)
 
+    def test_static_304_etag_modified_bug(self):
+        response1 = self.get_and_head("/static/robots.txt")
+        response2 = self.get_and_head("/static/robots.txt", headers={
+            'If-None-Match': '"MISMATCH"',
+            'If-Modified-Since': response1.headers['Last-Modified']})
+        self.assertEqual(response2.code, 200)
+
     def test_static_if_modified_since_pre_epoch(self):
         # On windows, the functions that work with time_t do not accept
         # negative values, and at least one client (processing.js) seems

pip install -e .[dev]
parse_log_pytest
pytest
pytest
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
certifi==2021.5.30
importlib-metadata @ file:///tmp/build/80754af9/importlib-metadata_1631916693255/work
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
more-itertools @ file:///tmp/build/80754af9/more-itertools_1637733554872/work
packaging @ file:///tmp/build/80754af9/packaging_1637314298585/work
pluggy @ file:///tmp/build/80754af9/pluggy_1615976315926/work
py @ file:///opt/conda/conda-bld/py_1644396412707/work
pyparsing @ file:///tmp/build/80754af9/pyparsing_1635766073266/work
pytest==6.2.4
toml @ file:///tmp/build/80754af9/toml_1616166611790/work
-e git+https://github.com/tornadoweb/tornado.git@15e350aa27493a98bf797d112af304b00c1d63c3#egg=tornado
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=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
prefix: /opt/conda/envs/tornado

tornado/test/web_test.py::StaticFileTest::test_static_304_etag_modified_bug
tornado/test/web_test.py::SecureCookieV1Test::test_arbitrary_bytes tornado/test/web_test.py::SecureCookieV1Test::test_cookie_tampering_future_timestamp tornado/test/web_test.py::SecureCookieV1Test::test_round_trip tornado/test/web_test.py::SecureCookieV2Test::test_key_version_increment_version tornado/test/web_test.py::SecureCookieV2Test::test_key_version_invalidate_version tornado/test/web_test.py::SecureCookieV2Test::test_key_version_roundtrip tornado/test/web_test.py::SecureCookieV2Test::test_key_version_roundtrip_differing_version tornado/test/web_test.py::SecureCookieV2Test::test_round_trip tornado/test/web_test.py::CookieTest::test_cookie_special_char tornado/test/web_test.py::CookieTest::test_get_cookie tornado/test/web_test.py::CookieTest::test_set_cookie tornado/test/web_test.py::CookieTest::test_set_cookie_domain tornado/test/web_test.py::CookieTest::test_set_cookie_expires_days tornado/test/web_test.py::CookieTest::test_set_cookie_false_flags tornado/test/web_test.py::CookieTest::test_set_cookie_max_age tornado/test/web_test.py::CookieTest::test_set_cookie_overwrite tornado/test/web_test.py::AuthRedirectTest::test_absolute_auth_redirect tornado/test/web_test.py::AuthRedirectTest::test_relative_auth_redirect tornado/test/web_test.py::ConnectionCloseTest::test_connection_close tornado/test/web_test.py::RequestEncodingTest::test_error tornado/test/web_test.py::RequestEncodingTest::test_group_encoding tornado/test/web_test.py::RequestEncodingTest::test_group_question_mark tornado/test/web_test.py::RequestEncodingTest::test_slashes tornado/test/web_test.py::WSGISafeWebTest::test_decode_argument tornado/test/web_test.py::WSGISafeWebTest::test_decode_argument_invalid_unicode tornado/test/web_test.py::WSGISafeWebTest::test_decode_argument_plus tornado/test/web_test.py::WSGISafeWebTest::test_get_argument tornado/test/web_test.py::WSGISafeWebTest::test_get_body_arguments tornado/test/web_test.py::WSGISafeWebTest::test_get_query_arguments tornado/test/web_test.py::WSGISafeWebTest::test_header_injection tornado/test/web_test.py::WSGISafeWebTest::test_multi_header tornado/test/web_test.py::WSGISafeWebTest::test_no_gzip tornado/test/web_test.py::WSGISafeWebTest::test_optional_path tornado/test/web_test.py::WSGISafeWebTest::test_redirect tornado/test/web_test.py::WSGISafeWebTest::test_reverse_url tornado/test/web_test.py::WSGISafeWebTest::test_types tornado/test/web_test.py::WSGISafeWebTest::test_uimodule_resources tornado/test/web_test.py::WSGISafeWebTest::test_uimodule_unescaped tornado/test/web_test.py::WSGISafeWebTest::test_web_redirect tornado/test/web_test.py::WSGISafeWebTest::test_web_redirect_double_slash tornado/test/web_test.py::NonWSGIWebTests::test_empty_flush tornado/test/web_test.py::NonWSGIWebTests::test_flow_control tornado/test/web_test.py::ErrorResponseTest::test_default tornado/test/web_test.py::ErrorResponseTest::test_failed_write_error tornado/test/web_test.py::ErrorResponseTest::test_write_error tornado/test/web_test.py::StaticFileTest::test_absolute_static_url tornado/test/web_test.py::StaticFileTest::test_absolute_version_exclusion tornado/test/web_test.py::StaticFileTest::test_include_host_override tornado/test/web_test.py::StaticFileTest::test_path_traversal_protection tornado/test/web_test.py::StaticFileTest::test_relative_version_exclusion tornado/test/web_test.py::StaticFileTest::test_root_static_path tornado/test/web_test.py::StaticFileTest::test_static_304_if_modified_since tornado/test/web_test.py::StaticFileTest::test_static_304_if_none_match tornado/test/web_test.py::StaticFileTest::test_static_404 tornado/test/web_test.py::StaticFileTest::test_static_compressed_files tornado/test/web_test.py::StaticFileTest::test_static_etag tornado/test/web_test.py::StaticFileTest::test_static_files tornado/test/web_test.py::StaticFileTest::test_static_head tornado/test/web_test.py::StaticFileTest::test_static_head_range tornado/test/web_test.py::StaticFileTest::test_static_if_modified_since_pre_epoch tornado/test/web_test.py::StaticFileTest::test_static_if_modified_since_time_zone tornado/test/web_test.py::StaticFileTest::test_static_invalid_range tornado/test/web_test.py::StaticFileTest::test_static_range_if_none_match tornado/test/web_test.py::StaticFileTest::test_static_unsatisfiable_range_invalid_start tornado/test/web_test.py::StaticFileTest::test_static_unsatisfiable_range_zero_suffix tornado/test/web_test.py::StaticFileTest::test_static_url tornado/test/web_test.py::StaticFileTest::test_static_with_range tornado/test/web_test.py::StaticFileTest::test_static_with_range_end_edge tornado/test/web_test.py::StaticFileTest::test_static_with_range_full_file tornado/test/web_test.py::StaticFileTest::test_static_with_range_full_past_end tornado/test/web_test.py::StaticFileTest::test_static_with_range_neg_end tornado/test/web_test.py::StaticFileTest::test_static_with_range_partial_past_end tornado/test/web_test.py::StaticDefaultFilenameTest::test_static_default_filename tornado/test/web_test.py::StaticDefaultFilenameTest::test_static_default_redirect tornado/test/web_test.py::StaticFileWithPathTest::test_serve tornado/test/web_test.py::CustomStaticFileTest::test_serve tornado/test/web_test.py::CustomStaticFileTest::test_static_url tornado/test/web_test.py::HostMatchingTest::test_host_matching tornado/test/web_test.py::DefaultHostMatchingTest::test_default_host_matching tornado/test/web_test.py::NamedURLSpecGroupsTest::test_named_urlspec_groups tornado/test/web_test.py::ClearHeaderTest::test_clear_header tornado/test/web_test.py::Header204Test::test_204_headers tornado/test/web_test.py::Header304Test::test_304_headers tornado/test/web_test.py::StatusReasonTest::test_status tornado/test/web_test.py::DateHeaderTest::test_date_header tornado/test/web_test.py::RaiseWithReasonTest::test_httperror_str tornado/test/web_test.py::RaiseWithReasonTest::test_httperror_str_from_httputil tornado/test/web_test.py::RaiseWithReasonTest::test_raise_with_reason tornado/test/web_test.py::ErrorHandlerXSRFTest::test_404_xsrf tornado/test/web_test.py::ErrorHandlerXSRFTest::test_error_xsrf tornado/test/web_test.py::GzipTestCase::test_gzip tornado/test/web_test.py::GzipTestCase::test_gzip_not_requested tornado/test/web_test.py::GzipTestCase::test_gzip_static tornado/test/web_test.py::GzipTestCase::test_vary_already_present tornado/test/web_test.py::GzipTestCase::test_vary_already_present_multiple tornado/test/web_test.py::PathArgsInPrepareTest::test_kw tornado/test/web_test.py::PathArgsInPrepareTest::test_pos tornado/test/web_test.py::ClearAllCookiesTest::test_clear_all_cookies tornado/test/web_test.py::ExceptionHandlerTest::test_http_error tornado/test/web_test.py::ExceptionHandlerTest::test_known_error tornado/test/web_test.py::ExceptionHandlerTest::test_unknown_error tornado/test/web_test.py::BuggyLoggingTest::test_buggy_log_exception tornado/test/web_test.py::UIMethodUIModuleTest::test_ui_method tornado/test/web_test.py::GetArgumentErrorTest::test_catch_error tornado/test/web_test.py::MultipleExceptionTest::test_multi_exception tornado/test/web_test.py::SetLazyPropertiesTest::test_set_properties tornado/test/web_test.py::GetCurrentUserTest::test_get_current_user_from_ui_module_is_lazy tornado/test/web_test.py::GetCurrentUserTest::test_get_current_user_from_ui_module_works tornado/test/web_test.py::GetCurrentUserTest::test_get_current_user_works tornado/test/web_test.py::UnimplementedHTTPMethodsTest::test_unimplemented_standard_methods tornado/test/web_test.py::UnimplementedNonStandardMethodsTest::test_unimplemented_other tornado/test/web_test.py::UnimplementedNonStandardMethodsTest::test_unimplemented_patch tornado/test/web_test.py::AllHTTPMethodsTest::test_standard_methods tornado/test/web_test.py::PatchMethodTest::test_other tornado/test/web_test.py::PatchMethodTest::test_patch tornado/test/web_test.py::FinishInPrepareTest::test_finish_in_prepare tornado/test/web_test.py::Default404Test::test_404 tornado/test/web_test.py::Custom404Test::test_404 tornado/test/web_test.py::DefaultHandlerArgumentsTest::test_403 tornado/test/web_test.py::HandlerByNameTest::test_handler_by_name tornado/test/web_test.py::StreamingRequestBodyTest::test_close_during_upload tornado/test/web_test.py::StreamingRequestBodyTest::test_early_return tornado/test/web_test.py::StreamingRequestBodyTest::test_early_return_with_data tornado/test/web_test.py::StreamingRequestBodyTest::test_streaming_body tornado/test/web_test.py::DecoratedStreamingRequestFlowControlTest::test_flow_control_chunked_body tornado/test/web_test.py::DecoratedStreamingRequestFlowControlTest::test_flow_control_compressed_body tornado/test/web_test.py::DecoratedStreamingRequestFlowControlTest::test_flow_control_fixed_body tornado/test/web_test.py::NativeStreamingRequestFlowControlTest::test_flow_control_chunked_body tornado/test/web_test.py::NativeStreamingRequestFlowControlTest::test_flow_control_compressed_body tornado/test/web_test.py::NativeStreamingRequestFlowControlTest::test_flow_control_fixed_body tornado/test/web_test.py::IncorrectContentLengthTest::test_content_length_too_high tornado/test/web_test.py::IncorrectContentLengthTest::test_content_length_too_low tornado/test/web_test.py::ClientCloseTest::test_client_close tornado/test/web_test.py::SignedValueTest::test_expired tornado/test/web_test.py::SignedValueTest::test_key_version_retrieval tornado/test/web_test.py::SignedValueTest::test_key_versioning_invalid_key tornado/test/web_test.py::SignedValueTest::test_key_versioning_read_write_default_key tornado/test/web_test.py::SignedValueTest::test_key_versioning_read_write_non_default_key tornado/test/web_test.py::SignedValueTest::test_known_values tornado/test/web_test.py::SignedValueTest::test_name_swap tornado/test/web_test.py::SignedValueTest::test_non_ascii tornado/test/web_test.py::SignedValueTest::test_payload_tampering tornado/test/web_test.py::SignedValueTest::test_signature_tampering tornado/test/web_test.py::XSRFTest::test_cross_user tornado/test/web_test.py::XSRFTest::test_distinct_tokens tornado/test/web_test.py::XSRFTest::test_refresh_token tornado/test/web_test.py::XSRFTest::test_versioning tornado/test/web_test.py::XSRFTest::test_xsrf_fail_argument_invalid_format tornado/test/web_test.py::XSRFTest::test_xsrf_fail_body_no_cookie tornado/test/web_test.py::XSRFTest::test_xsrf_fail_cookie_invalid_format tornado/test/web_test.py::XSRFTest::test_xsrf_fail_cookie_no_body tornado/test/web_test.py::XSRFTest::test_xsrf_fail_no_token tornado/test/web_test.py::XSRFTest::test_xsrf_success_header tornado/test/web_test.py::XSRFTest::test_xsrf_success_non_hex_token tornado/test/web_test.py::XSRFTest::test_xsrf_success_post_body tornado/test/web_test.py::XSRFTest::test_xsrf_success_query_string tornado/test/web_test.py::XSRFTest::test_xsrf_success_short_token tornado/test/web_test.py::XSRFCookieKwargsTest::test_xsrf_httponly tornado/test/web_test.py::FinishExceptionTest::test_finish_exception tornado/test/web_test.py::DecoratorTest::test_addslash tornado/test/web_test.py::DecoratorTest::test_removeslash tornado/test/web_test.py::CacheTest::test_multiple_strong_etag_match tornado/test/web_test.py::CacheTest::test_multiple_strong_etag_not_match tornado/test/web_test.py::CacheTest::test_multiple_weak_etag_match tornado/test/web_test.py::CacheTest::test_multiple_weak_etag_not_match tornado/test/web_test.py::CacheTest::test_strong_etag_match tornado/test/web_test.py::CacheTest::test_strong_etag_not_match tornado/test/web_test.py::CacheTest::test_weak_etag_match tornado/test/web_test.py::CacheTest::test_weak_etag_not_match tornado/test/web_test.py::CacheTest::test_wildcard_etag tornado/test/web_test.py::RequestSummaryTest::test_missing_remote_ip tornado/test/web_test.py::HTTPErrorTest::test_copy tornado/test/web_test.py::ApplicationTest::test_listen tornado/test/web_test.py::URLSpecReverseTest::test_non_reversible tornado/test/web_test.py::URLSpecReverseTest::test_reverse tornado/test/web_test.py::URLSpecReverseTest::test_reverse_arguments tornado/test/web_test.py::RedirectHandlerTest::test_basic_redirect tornado/test/web_test.py::RedirectHandlerTest::test_redirect_pattern tornado/test/web_test.py::RedirectHandlerTest::test_redirect_with_appending_argument tornado/test/web_test.py::RedirectHandlerTest::test_redirect_with_argument
tornado/web.py
tornado/web.py