Repository: LMFDB/lmfdb

Issue:
Disable cached function?
```
Exception on /EllipticCurve/browse/4/0/ [GET]
Traceback (most recent call last):
  File "sage/misc/cachefunc.pyx", line 1943, in sage.misc.cachefunc.CachedMethodCaller.__call__ (build/cythonized/sage/misc/cachefunc.c:10303)
    return cache[k]
KeyError: (((0, 2),), ())

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/ecnf/main.py", line 628, in statistics_by_signature
    info['summary'] = ECNF_stats().signature_summary(sig)
  File "sage/misc/cachefunc.pyx", line 1948, in sage.misc.cachefunc.CachedMethodCaller.__call__ (build/cythonized/sage/misc/cachefunc.c:10437)
    w = self._instance_call(*args, **kwds)
  File "sage/misc/cachefunc.pyx", line 1824, in sage.misc.cachefunc.CachedMethodCaller._instance_call (build/cythonized/sage/misc/cachefunc.c:9916)
    return self.f(self._instance, *args, **kwds)
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/ecnf/ecnf_stats.py", line 218, in signature_summary
    stats = self.sig_normstats[r,s]
KeyError: (0, 2)
[2021-12-01 13:23:50 UTC] 500 error on URL https://beta.lmfdb.org/EllipticCurve/browse/4/0/ ()
Exception on /EllipticCurve/browse/4/2/ [GET]
Traceback (most recent call last):
  File "sage/misc/cachefunc.pyx", line 1943, in sage.misc.cachefunc.CachedMethodCaller.__call__ (build/cythonized/sage/misc/cachefunc.c:10303)
    return cache[k]
KeyError: (((2, 1),), ())

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/ecnf/main.py", line 628, in statistics_by_signature
    info['summary'] = ECNF_stats().signature_summary(sig)
  File "sage/misc/cachefunc.pyx", line 1948, in sage.misc.cachefunc.CachedMethodCaller.__call__ (build/cythonized/sage/misc/cachefunc.c:10437)
    w = self._instance_call(*args, **kwds)
  File "sage/misc/cachefunc.pyx", line 1824, in sage.misc.cachefunc.CachedMethodCaller._instance_call (build/cythonized/sage/misc/cachefunc.c:9916)
    return self.f(self._instance, *args, **kwds)
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/ecnf/ecnf_stats.py", line 218, in signature_summary
    stats = self.sig_normstats[r,s]
KeyError: (2, 1)
[2021-12-01 13:23:55 UTC] 500 error on URL https://beta.lmfdb.org/EllipticCurve/browse/4/2/ ()
```

I also observed similar error before and that is why I did this PR https://github.com/LMFDB/lmfdb/pull/4844


Discussion:
If you think that there is a bug in Sage's cached_method decorator then it should be reported to sage-devel.  Is that the issue?
No, I don't think is a problem in Sage, but a problem of using the `cached_method` with gunicorn.
Is this sill an issue?
Yes, just tried the same page again and it failed again for the same reason:
```
Exception on /EllipticCurve/browse/4/0/ [GET]
Traceback (most recent call last):
  File "sage/misc/cachefunc.pyx", line 1943, in sage.misc.cachefunc.CachedMethodCaller.__call__ (build/cythonized/sage/misc/cachefunc.c:10303)
    return cache[k]
KeyError: (((0, 2),), ())

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/sage/sage-9.4/local/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/ecnf/main.py", line 656, in statistics_by_signature
    info['summary'] = ECNF_stats().signature_summary(sig)
  File "sage/misc/cachefunc.pyx", line 1948, in sage.misc.cachefunc.CachedMethodCaller.__call__ (build/cythonized/sage/misc/cachefunc.c:10437)
    w = self._instance_call(*args, **kwds)
  File "sage/misc/cachefunc.pyx", line 1824, in sage.misc.cachefunc.CachedMethodCaller._instance_call (build/cythonized/sage/misc/cachefunc.c:9916)
    return self.f(self._instance, *args, **kwds)
  File "/home/lmfdb/lmfdb-git-dev/lmfdb/ecnf/ecnf_stats.py", line 218, in signature_summary
    stats = self.sig_normstats[r,s]
KeyError: (0, 2)
[2022-01-19 23:58:35 UTC] 500 error on URL https://beta.lmfdb.org/EllipticCurve/browse/4/0/ ()


Resolving patch:
diff --git a/lmfdb/ecnf/ecnf_stats.py b/lmfdb/ecnf/ecnf_stats.py
--- a/lmfdb/ecnf/ecnf_stats.py
+++ b/lmfdb/ecnf/ecnf_stats.py
@@ -214,6 +214,8 @@ def field_summary(self, field):
     def signature_summary(self, sig):
         r, s = sig
         d = r+2*s
+        if sig not in self.sig_normstats:
+            return ''
         stats = self.sig_normstats[r,s]
         ncurves = stats['ncurves']
         nclasses = stats['nclasses']
diff --git a/lmfdb/ecnf/main.py b/lmfdb/ecnf/main.py
--- a/lmfdb/ecnf/main.py
+++ b/lmfdb/ecnf/main.py
@@ -744,12 +744,14 @@ def statistics_by_signature(d,r):
     else:
         info['degree'] = d
 
-    if r not in range(d%2,d+1,2):
-        info['error'] = "Invalid signature %s" % info['sig']
     s = (d-r)//2
     sig = (r,s)
     info['sig'] = '%s,%s' % sig
+    if r not in range(d%2,d+1,2):
+        info['error'] = "Invalid signature %s" % info['sig']
     info['summary'] = ECNF_stats().signature_summary(sig)
+    if not info['summary']:
+        info['error'] = "The database does not contain any curves defined over fields of signature %s" % info['sig']
 
     fields_by_sig = ECNF_stats().fields_by_sig
     counts_by_field = ECNF_stats().field_normstats


Test patch: