Repository navigation
Cache key 51D_ values, per-visitor cache tests, thread-safe LRU cache and a warning for a cloud engine cache - #301
Merged
Conversation
…e cache The Cloud Request Engine sends a request carrying a session id, a sequence, a JA4 or HTTP/2 settings to the cloud service whatever its cache holds, and stores no answer to it. The existing tests supplied the JA4 and HTTP/2 settings together, or the session id and sequence together, so removing one name from Constants.UNCACHED_EVIDENCE_NAMES left every test passing. - test_cloudrequestengine_cache.py tests each of the four values alone, under two spellings each (another prefix and another letter case), with no other value made for one visitor. Each request reaches the cloud service carrying the value, is not answered from a cache that holds an entry for its evidence, and leaves nothing in the cache, so the same request again reaches the cloud service too. - test_builder_cache.py in fiftyone_devicedetection_cloud builds the Device Detection Cloud Pipeline Builder with its defaults, the JavaScript builder included, and a cache. Three identical requests each reach the cloud service, because the sequence element gives every request a session id and a sequence, and the cache stays empty. Each new test fails when its name is removed from the list. The builder test fails when both the session id and the sequence are removed, as either one alone still sends the request to the cloud service.
…alue A page's scripts send their answers under a name that starts 51D_ and changes with each script, so the cloud service lists none of them among its evidence keys. The engine's cache key was made from the listed keys alone, so a request carrying answers but no session id, sequence, JA4 or HTTP/2 settings (with add_sequence_element off, or from a caller's own route) was answered from the cache with the response to other answers, and its own answers never reached the cloud service. The engine now accepts, beside the keys the service lists, every query or cookie value whose name starts 51D_ in any letter case, the bare name 51D_ included, as the Rust Cloud Request Engine does. CloudEvidenceKeyFilter adds the answers to the listed keys, and its list still holds the listed keys alone, because the answers have no names to list, so the Vary header the web integration writes is unchanged. What the engine sends is unchanged too, as it already sent every evidence value. Being accepted evidence, the answers are now part of the ETag of the web integration's client-side responses, which changes with them. Tests in test_cloudrequestengine_cache.py, each failing before this change: - Two requests that differ only in a 51D_ value, under four spellings, each reach the cloud service and get their own answer, and the same value again is answered from the cache. - The filter accepts the 51D_ query and cookie names and refuses a header, a server value, a name that only contains 51D_, a key with no prefix and a name whose first character is not ASCII. test_discovery_at_build.py now watches CloudEvidenceKeyFilter, the class the engine makes, to show a second request makes no filter.
The server route test checked the dictionary MockRequestClient records, and its 51D_ values held no character a form body encodes, so it could not show the values reach the cloud service unchanged. - The posted answer and the cookie answer now carry +, %, =, a space and characters outside ASCII, through the Flask application and the web integration as before. - WireClient answers as MockRequestClient does and also gives every request to RequestClient, the client the engine uses by default, with the network replaced, keeping the URL, the content type and the body it would send. - The test checks each data request's body is a form body of ASCII text, that reading it as the cloud service does (UTF-8) gives back each answer, the cookie answer and the session id exactly, once each, and that each value is form encoded, with + as %2B, % as %25, = as %3D, a space as + and each byte of the UTF-8 of a character outside ASCII percent encoded. The test fails if RequestClient joins the body without encoding it, and if the engine decodes a value before sending it.
One LRUEngineCache is shared by every request an engine processes, and a web server processes requests on several threads at once. The cachetools LRUCache underneath changes its order on every read and removes the least recently used entry on a write when full, with nothing holding threads apart, so concurrent requests could raise KeyError or "OrderedDict mutated during iteration" from inside the cache, or leave it inconsistent. get_cache_value and set_cache_value now hold a threading.Lock while they read or write the LRUCache, so one thread at a time is inside it. test_lru_cache.py in fiftyone_pipeline_engines, both failing without the lock on three runs out of three: - Eight threads read and write one cache whose LRUCache pauses in each call and records how many threads are inside it at once, which is one. - Eight threads make 3,000 reads and writes each on one cache of 8 entries over 32 keys, with the interpreter switching threads every microsecond. No thread raises, every read gives the value written for its key or nothing, the cache holds no more than 8 entries, and it still evicts the least recently used entry afterwards.
test_javascriptbuilder_object_name.py had LF line endings until the change that kept two blank lines before _declared_names, which also turned every line ending to CRLF, so that change showed as all 350 lines rewritten. The file has LF line endings again, as every other Python file here does. With white space ignored, the file differs from its version before the Node start-up change only in setUpModule and the two constants, NODE_CALL_SECONDS and NODE_START_SECONDS. Against the first commit of that change, the only difference is the blank line the second one added.
…mples After a run of Publish or Nightly Pipeline, the Examples vs Published Packages workflow pinned each package to what PyPI's JSON gave as its latest version at that moment, which straight after an upload can still be the version before, so the examples could be tested against the previous release with nothing to say so beyond the summary table. ci/publish-package.ps1 pushes the tag '<package>/<version>' on the published commit once each package is uploaded. The workflow now reads the tags on that commit (git ls-remote, as the repository is public) and pins each package the publish released to the version its tag names, waiting until PyPI's simple index, which pip reads, lists that version. It asks every $PyPIRetrySeconds (15) seconds and fails the run, naming the package and version, after $PyPIWaitSeconds (900) seconds. A package with no tag on that commit was not released by the publish, so it is pinned to its latest version as before, and a version given by hand is used as before. Run locally against PyPI with the step's script: a commit with no tags pinned every package to its latest version, a version given by hand was used for every package, and the 4.6.1 commit pinned the six pipeline packages to 4.6.1 at once and failed, with a short wait, on fiftyone-devicedetection-shared 4.6.1, which PyPI no longer lists.
… raise CloudEngine.set_cache raised an exception, so code that gave a cloud engine a cache, which worked before, would stop at start-up after a patch release. A cloud engine accepts no evidence, so there is no key to use in a cache of its own, and the cache belongs on the Cloud Request Engine, where the pipeline builders now put it. set_cache on a CloudEngine, and so on DerivedCloud and every engine built on CloudEngine, now accepts the call and keeps no cache. It logs one warning for each engine, however often the engine is given a cache, naming the engine by its class and data key and saying to add the cache to the Cloud Request Engine. The warning goes to the logger of each pipeline the engine is in, or to the module's own logger (fiftyone_pipeline_cloudrequestengine.cloudengine) while it is in none, and nothing is logged as requests are processed. CloudEngine.process still never uses a cache. This is how the Rust cloud engines treat a cache setting, which is read and has no effect, with a warning logged. Tests, the first three failing while set_cache raised: - test_cloudengine_cache.py: a cache given before the engine is in a pipeline is accepted with one warning on the module's logger, and one given once the engine is in a pipeline with one warning on the pipeline's logger. In both, the engine keeps no cache, three requests each get their own value, the cache is never read or written and nothing more is logged. A cache assigned to the engine directly is never read or written either. - test_human_confidence.py: DerivedCloud accepts a cache with one warning naming it, and a cache given through set_cache or assigned directly never serves a redeemed result and is never read or written. Each warning test fails if the warning is logged on every call.
This was referenced Dec 1, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Behaviour fixes and missing tests found while checking rc (802efb1, after #300) before the next release. One commit each, listed in order. Every commit keeps LF line endings, and the only file whose line endings change is the one restored to LF below.
What changes
Each value made for one visitor is proved to keep a request out of the cache (8f18579, tests only).
The existing tests supplied the JA4 and HTTP/2 settings together, or the session id and sequence together, so removing one name from
Constants.UNCACHED_EVIDENCE_NAMESleft every test passing.pipeline/fiftyone_pipeline_cloudrequestengine/tests/test_cloudrequestengine_cache.pylines 325 to 337 test the session id, the sequence, the JA4 and the HTTP/2 settings each alone, under two spellings (another prefix and another letter case), with no other value made for one visitor. Each request reaches the cloud service carrying the value, is not answered from a cache that holds an entry for its evidence, and leaves nothing in the cache.device-detection/fiftyone_devicedetection_cloud/tests/test_builder_cache.pyline 115 builds the Device Detection Cloud Pipeline Builder with its defaults, the JavaScript builder included, and a cache. Three identical requests each reach the cloud service and the cache stays empty, because the sequence element gives every request a session id and a sequence.The Cloud Request Engine's cache key holds every 51D_ query and cookie value (96f6bb6).
A page's scripts send their answers under a name that starts
51D_and changes with each script, so the cloud service lists none of them among its evidence keys, and the cache key was made from the listed keys alone. A request carrying answers but no session id, sequence, JA4 or HTTP/2 settings (withadd_sequence_elementoff, or from a caller's own route) was answered from the cache with the response to other answers.cloudrequestengine.pylines 119 to 148 addis_answer_keyandCloudEvidenceKeyFilter, which the engine now makes at line 610. The engine accepts, beside the listed keys, every query or cookie value whose name starts51D_in any letter case, the bare name51D_included, and so these values are part of the cache key. The constants are inconstants.pylines 155 to 161.is_answer_keyandAcceptedEvidence). In Python the filter only exists once the keys are known, as before.liststill holds the listed keys alone, so theVaryheader the web integration writes is unchanged. What the engine sends is unchanged, as it already sent every evidence value. Being accepted evidence, the 51D_ values are now part of the ETag of the web integration's client-side responses.test_cloudrequestengine_cache.pylines 339 and 359, both failing before the change: two requests that differ only in a 51D_ value each reach the cloud service and get their own answer, and the filter accepts and refuses the same keys as the Rust tests do.test_discovery_at_build.pynow watchesCloudEvidenceKeyFilter, the class the engine makes.The server route test checks 51D_ values unchanged on the wire (40f504f, tests only).
test_server_route_pass_through.py: the posted answer and the cookie answer now carry+,%,=, a space and characters outside ASCII.WireClient(line 80) gives every request toRequestClient, the engine's default client, with the network replaced, and keeps the body it would send.test_session_id_sequence_and_answers_reach_the_cloud(line 217) checks each body is a form body of ASCII text, that reading it as UTF-8 form data gives back each value exactly, once, and that each value is form encoded (+as%2B,%as%25,=as%3D, a space as+, UTF-8 bytes percent encoded). The test fails ifRequestClientjoins the body without encoding it, and if the engine decodes a value before sending it.LRUEngineCacheis safe to share between threads (7a018ec).pipeline/fiftyone_pipeline_engines/src/fiftyone_pipeline_engines/lru_cache.pylines 44 to 54 hold athreading.Lockaround each read and write of the cachetoolsLRUCache, which reorders on every read and evicts on a write. Without it, concurrent requests raisedKeyErrorandRuntimeError: OrderedDict mutated during iterationfrom inside the cache.pipeline/fiftyone_pipeline_engines/tests/test_lru_cache.pyhas eight threads hammering one cache, with a cache that records the most threads inside it at once (one) and a run of 24,000 reads and writes over more keys than the cache holds. Both tests failed on three runs out of three without the lock.test_javascriptbuilder_object_name.pyhas LF line endings again (d26c2c9).The change that kept two blank lines before
_declared_names(Start Node once before the object name tests, outside their time limit #296) also turned every line ending to CRLF. With white space ignored, the file now differs from its version before Start Node once before the object name tests, outside their time limit #296 only insetUpModuleand the two constantsNODE_CALL_SECONDSandNODE_START_SECONDS.Examples vs Published Packages waits for the version a publish released (b7fda59).
After a run of Publish or Nightly Pipeline,
.github/workflows/examples-published.ymlpinned each package to what PyPI's JSON gave as its latest version at that moment, which straight after an upload can still be the version before.ci/publish-package.ps1pushes the tag<package>/<version>on the published commit once each package is uploaded, so the step "Choose the published versions" (line 160) now reads the tags onworkflow_run.head_shawithgit ls-remote, pins each package the publish released to its tagged version, and waits until PyPI's simple index (what pip reads) lists it, asking every$PyPIRetrySeconds(15) seconds for at most$PyPIWaitSeconds(900) seconds, then fails naming the package. A package with no tag on that commit was not released, so it takes its latest version as before, and a version given by hand is used as before. Run locally against PyPI: a commit with no tags pinned every package to its latest version, a version given by hand was used throughout, and the 4.6.1 commit pinned the six pipeline packages to 4.6.1 at once and failed (with a short wait) onfiftyone-devicedetection-shared4.6.1, which PyPI no longer lists.A cache given to a cloud engine is ignored with a warning, so existing code keeps starting (155cc98).
Key the cloud engines' cache on the request's evidence and apply the Cloud Request Engine's cache rules #300 made
CloudEngine.set_cacheraise, which would stop code that gave a cloud engine a cache, and worked before, at start-up after a patch release.cloudengine.pyline 104:set_cacheon aCloudEngine, and so onDerivedCloudand every engine built on it, now accepts the call and keeps no cache, and logs one warning per engine naming it (class and data key) and saying to add the cache to the Cloud Request Engine. The warning goes to the logger of each pipeline the engine is in, or to the module's loggerfiftyone_pipeline_cloudrequestengine.cloudenginewhile it is in none, and nothing is logged per request.CloudEngine.processstill never uses a cache. This matches the Rust cloud engines, where a cache setting is read and has no effect, with a warning logged. The tests from Key the cloud engines' cache on the request's evidence and apply the Cloud Request Engine's cache rules #300 that asserted the raise (test_cloudengine_cache.py,test_human_confidence.py) now assert the call succeeds, the warning is logged once, the cache is never read or written and each request gets its own values. They failed whileset_cacheraised, and the warning tests fail if the warning is logged on every call. No builder test asserted the raise.Removing a name from the bypass list
Each new test in item 1 was run with
Constants.UNCACHED_EVIDENCE_NAMESedited and then restored.session-idtest_a_session_id_alone_is_sent_and_never_cached(both spellings)sequencetest_a_sequence_alone_is_sent_and_never_cached(both spellings)ja4test_a_ja4_alone_is_sent_and_never_cached(both spellings)h2test_http2_settings_alone_are_sent_and_never_cached(both spellings)session-idandsequencetest_with_the_javascript_builder_each_request_reaches_the_cloudThe builder test fails only when both are removed, because the sequence element sets both and either one alone sends the request to the cloud service.
51D_ values whose names collide
Not changed. The Rust package also sends one value in both cases.
build_content), as Python'sadd_query_datadoes.insert), and its web integration adds the pairs in order, so the last value wins (build_evidence). Python'swebevidencereadsrequest.args.items(), which keeps the first value. Both send one value. Which of the repeated values is kept differs, and is left as it is here.Local test runs
Python 3.12.10 on Windows, each package's tests from its folder with
python -m pytest -m "not live and not staging", the packages installed editable from this branch.No test failed. The on-premise packages and the Device Detection and IP Intelligence examples were not run here, as they need the native builds and data files, and the live tests need the CI keys, so CI is their check.
The Examples vs Published Packages check runs on this pull request because it changes that workflow. On a pull request it tests the examples against the packages already on PyPI, which do not have rc's changes yet, so it is expected to fail here as it did on #294 and #298.