Mercurial > hg
annotate tests/dumbhttp.py @ 47010:76ae43d5b1db stable
repoview: fix memory leak of filtered repo classes
The leak occurs in long-running server processes with
extensions, and is measured at 110kB per request.
Before this change, the contents of the `_filteredrepotypes`
cache are not properly garbage collected, despite it begin
a `WeakKeyDictionary`.
Extensions have a tendency to generate a new repository class
for each `localrepo` instantiation. Server processes based
on `hgwebdir_mod` will instantiate a new `localrepo` for each
HTTP request that involves a repository.
As a result, with a testing process that repeatedly opens a
repository with several extensions activated
(`topic` notably among them), we see a steady increase in
resident memory of 110kB per repository instantiation before this
change. This is also true, if we call `gc.collect()` at each
instantiation, like `hgwebdir_mod` does, or not.
The cause of the leak is that the *values* aren't weak references.
This change uses `weakref.ref` for the values, and this makes
in our measurements the resident size increase drop to 5kB per
repository instantiation, with no explicit call of `gc.collect()`
at all.
There is currently no reason to believe that this remaining leak
of 5kB is related to or even due to Mercurial core.
We've also seen evidence that `ui.ui` instances weren't properly
garbage collected before the change (with the change, they are).
This could explain why the figures are relatively high.
In theory, the collection of weak references could lead to
much more misses in the cache, so we measured the impact on
the original case that was motivation for introducing that cache
in 7e89bd0cfb86 (see also issue5043): `hg convert` of the
mozilla-central repository. The bad news here is that there is a
major memory leak there, both with and without the present changeset.
There were no more cache misses, and we could see no
more memory leak with this change: the resident size after importing
roughly 100000 changesets was at 12.4GB before, and 12.5GB after.
The small increase is mentioned for completeness only, and we
believe that it should be ignored, at least as long as the main
leak isn't fixed. At less than 1% of the main leak, even finding out
whether it is merely noise would be wasteful.
Original context where this was spotted and first mitigated:
https://foss.heptapod.net/heptapod/heptapod/-/issues/466
The leak reduction was also obtained in Heptapod inner HTTP server,
which amounts to the same as `hgwebdir_mod` for these questions.
The measurements done with Python 3.9, similar figures seen with 3.8.
More work on our side would be needed to give measurements with 2.7,
because of testing server process does not support it.
author | Georges Racinet <georges.racinet@octobus.net> |
---|---|
date | Fri, 23 Apr 2021 18:30:53 +0200 |
parents | c102b704edb5 |
children | 23f5ed6dbcb1 |
rev | line source |
---|---|
45830
c102b704edb5
global: use python3 in shebangs
Gregory Szorc <gregory.szorc@gmail.com>
parents:
43076
diff
changeset
|
1 #!/usr/bin/env python3 |
22959
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
2 |
27282
0bb8c405a7c7
tests/dumbhttp: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23136
diff
changeset
|
3 from __future__ import absolute_import |
0bb8c405a7c7
tests/dumbhttp: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23136
diff
changeset
|
4 |
22959
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
5 """ |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
6 Small and dumb HTTP server for use in tests. |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
7 """ |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
8 |
27282
0bb8c405a7c7
tests/dumbhttp: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23136
diff
changeset
|
9 import optparse |
31004
d05fefbb5ab3
dumbhttp: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents:
30506
diff
changeset
|
10 import os |
27282
0bb8c405a7c7
tests/dumbhttp: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23136
diff
changeset
|
11 import signal |
31004
d05fefbb5ab3
dumbhttp: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents:
30506
diff
changeset
|
12 import socket |
27282
0bb8c405a7c7
tests/dumbhttp: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23136
diff
changeset
|
13 import sys |
22959
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
14 |
27282
0bb8c405a7c7
tests/dumbhttp: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23136
diff
changeset
|
15 from mercurial import ( |
38593
e46c3b6a47b5
py3: fix dumbhttp.py to convert --daemon-postexec arguments back to bytes
Yuya Nishihara <yuya@tcha.org>
parents:
37672
diff
changeset
|
16 encoding, |
37672
8bacc09814ba
py3: make values bytes before passing into server.runservice()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
34944
diff
changeset
|
17 pycompat, |
30506
d9d8d78e6bc9
server: move cmdutil.service() to new module (API)
Yuya Nishihara <yuya@tcha.org>
parents:
29566
diff
changeset
|
18 server, |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28771
diff
changeset
|
19 util, |
27282
0bb8c405a7c7
tests/dumbhttp: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23136
diff
changeset
|
20 ) |
0bb8c405a7c7
tests/dumbhttp: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23136
diff
changeset
|
21 |
29566
075146e85bb6
py3: conditionalize BaseHTTPServer, SimpleHTTPServer and CGIHTTPServer import
Pulkit Goyal <7895pulkit@gmail.com>
parents:
28771
diff
changeset
|
22 httpserver = util.httpserver |
27282
0bb8c405a7c7
tests/dumbhttp: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
23136
diff
changeset
|
23 OptionParser = optparse.OptionParser |
22959
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
24 |
31004
d05fefbb5ab3
dumbhttp: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents:
30506
diff
changeset
|
25 if os.environ.get('HGIPV6', '0') == '1': |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
26 |
31004
d05fefbb5ab3
dumbhttp: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents:
30506
diff
changeset
|
27 class simplehttpserver(httpserver.httpserver): |
d05fefbb5ab3
dumbhttp: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents:
30506
diff
changeset
|
28 address_family = socket.AF_INET6 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
29 |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
30 |
31004
d05fefbb5ab3
dumbhttp: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents:
30506
diff
changeset
|
31 else: |
d05fefbb5ab3
dumbhttp: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents:
30506
diff
changeset
|
32 simplehttpserver = httpserver.httpserver |
d05fefbb5ab3
dumbhttp: use IPv6 if HGIPV6 is set to 1
Jun Wu <quark@fb.com>
parents:
30506
diff
changeset
|
33 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
34 |
34944
bf2db35a6fe7
test-static-http: flush access log per request
Yuya Nishihara <yuya@tcha.org>
parents:
34925
diff
changeset
|
35 class _httprequesthandler(httpserver.simplehttprequesthandler): |
bf2db35a6fe7
test-static-http: flush access log per request
Yuya Nishihara <yuya@tcha.org>
parents:
34925
diff
changeset
|
36 def log_message(self, format, *args): |
bf2db35a6fe7
test-static-http: flush access log per request
Yuya Nishihara <yuya@tcha.org>
parents:
34925
diff
changeset
|
37 httpserver.simplehttprequesthandler.log_message(self, format, *args) |
bf2db35a6fe7
test-static-http: flush access log per request
Yuya Nishihara <yuya@tcha.org>
parents:
34925
diff
changeset
|
38 sys.stderr.flush() |
bf2db35a6fe7
test-static-http: flush access log per request
Yuya Nishihara <yuya@tcha.org>
parents:
34925
diff
changeset
|
39 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
40 |
23136
6eab50a34fed
tests: have dumbhttp.py use cmdutil.service() to wait for child to listen()
Yuya Nishihara <yuya@tcha.org>
parents:
22959
diff
changeset
|
41 class simplehttpservice(object): |
6eab50a34fed
tests: have dumbhttp.py use cmdutil.service() to wait for child to listen()
Yuya Nishihara <yuya@tcha.org>
parents:
22959
diff
changeset
|
42 def __init__(self, host, port): |
6eab50a34fed
tests: have dumbhttp.py use cmdutil.service() to wait for child to listen()
Yuya Nishihara <yuya@tcha.org>
parents:
22959
diff
changeset
|
43 self.address = (host, port) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
44 |
23136
6eab50a34fed
tests: have dumbhttp.py use cmdutil.service() to wait for child to listen()
Yuya Nishihara <yuya@tcha.org>
parents:
22959
diff
changeset
|
45 def init(self): |
34944
bf2db35a6fe7
test-static-http: flush access log per request
Yuya Nishihara <yuya@tcha.org>
parents:
34925
diff
changeset
|
46 self.httpd = simplehttpserver(self.address, _httprequesthandler) |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
47 |
23136
6eab50a34fed
tests: have dumbhttp.py use cmdutil.service() to wait for child to listen()
Yuya Nishihara <yuya@tcha.org>
parents:
22959
diff
changeset
|
48 def run(self): |
6eab50a34fed
tests: have dumbhttp.py use cmdutil.service() to wait for child to listen()
Yuya Nishihara <yuya@tcha.org>
parents:
22959
diff
changeset
|
49 self.httpd.serve_forever() |
22959
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
50 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
51 |
22959
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
52 if __name__ == '__main__': |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
53 parser = OptionParser() |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
54 parser.add_option( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
55 '-p', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
56 '--port', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
57 dest='port', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
58 type='int', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
59 default=8000, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
60 help='TCP port to listen on', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
61 metavar='PORT', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
62 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
63 parser.add_option( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
64 '-H', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
65 '--host', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
66 dest='host', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
67 default='localhost', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
68 help='hostname or IP to listen on', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
69 metavar='HOST', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
70 ) |
34925
8b95e420e248
test-static-http: show all files accessed over HTTP
Yuya Nishihara <yuya@tcha.org>
parents:
31004
diff
changeset
|
71 parser.add_option('--logfile', help='file name of access/error log') |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
72 parser.add_option( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
73 '--pid', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
74 dest='pid', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
75 help='file name where the PID of the server is stored', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
76 ) |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
77 parser.add_option( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
78 '-f', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
79 '--foreground', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
80 dest='foreground', |
22959
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
81 action='store_true', |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
82 help='do not start the HTTP server in the background', |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
83 ) |
28451
c90cfe76e024
serve: accept multiple values for --daemon-postexec
Jun Wu <quark@fb.com>
parents:
28194
diff
changeset
|
84 parser.add_option('--daemon-postexec', action='append') |
22959
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
85 |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
86 (options, args) = parser.parse_args() |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
87 |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
88 signal.signal(signal.SIGTERM, lambda x, y: sys.exit(0)) |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
89 |
34925
8b95e420e248
test-static-http: show all files accessed over HTTP
Yuya Nishihara <yuya@tcha.org>
parents:
31004
diff
changeset
|
90 if options.foreground and options.logfile: |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
91 parser.error( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
92 "options --logfile and --foreground are mutually " "exclusive" |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
93 ) |
22959
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
94 if options.foreground and options.pid: |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
95 parser.error("options --pid and --foreground are mutually exclusive") |
10116463b0b1
tests: pull common http server setup out of individual tests
Mike Hommey <mh@glandium.org>
parents:
diff
changeset
|
96 |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
97 opts = { |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
98 b'pid_file': options.pid, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
99 b'daemon': not options.foreground, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
100 b'daemon_postexec': pycompat.rapply( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
101 encoding.strtolocal, options.daemon_postexec |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
102 ), |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
103 } |
23136
6eab50a34fed
tests: have dumbhttp.py use cmdutil.service() to wait for child to listen()
Yuya Nishihara <yuya@tcha.org>
parents:
22959
diff
changeset
|
104 service = simplehttpservice(options.host, options.port) |
37672
8bacc09814ba
py3: make values bytes before passing into server.runservice()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
34944
diff
changeset
|
105 runargs = [sys.executable, __file__] + sys.argv[1:] |
8bacc09814ba
py3: make values bytes before passing into server.runservice()
Pulkit Goyal <7895pulkit@gmail.com>
parents:
34944
diff
changeset
|
106 runargs = [pycompat.fsencode(a) for a in runargs] |
43076
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
107 server.runservice( |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
108 opts, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
109 initfn=service.init, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
110 runfn=service.run, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
111 logfile=options.logfile, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
112 runargs=runargs, |
2372284d9457
formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents:
38593
diff
changeset
|
113 ) |