A while back we added tests to make sure we weren't leaking hiwire keys. As a special case of this, we also were testing that we weren't leaking JsProxies. In the future, I would like to add tests to show that we aren't leaking PyProxies either. Currently we leak quite a lot of them.

Before we actually add any tests to show that we aren't leaking proxies, I'd like to rework the model of the PyProxy lifetime. The planned changes should make it far easier to actually avoid these leaks while still using a reasonably natural coding style.

This change has no user-facing effect: the tracing is disabled by default and the API to enable it is private and always will be.
Add support for pyproxy allocation tracing. A while back we added tests to make sure we weren't leaking hiwire keys. As a special case of this, we also were testing that we weren't leaking JsProxies. In the future, I would like to add tests to show that we aren't leaking PyProxies either. Currently we leak quite a lot of them.

Before we actually add any tests to show that we aren't leaking proxies, I'd like to rework the model of the PyProxy lifetime. The planned changes should make it far easier to actually avoid these leaks while still using a reasonably natural coding style.

This change has no user-facing effect: the tracing is disabled by default and the API to enable it is private and always will be.
import os
import sys
import argparse
import http.server
import socketserver
import pathlib

BUILD_PATH = pathlib.Path(__file__).resolve().parents[2] / "build"


class Handler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        self.send_header("Access-Control-Allow-Origin", "*")
        super().end_headers()


Handler.extensions_map[".wasm"] = "application/wasm"


def make_parser(parser):
    parser.description = "Start a server with the supplied " "build_dir and port."
    parser.add_argument(
        "--build_dir",
        action="store",
        type=str,
        default=BUILD_PATH,
        help="set the build directory",
    )
    parser.add_argument(
        "--port", action="store", type=int, default=8000, help="set the PORT number"
    )
    return parser


def server(port):
    httpd = socketserver.TCPServer(("", port), Handler)
    return httpd


def main(args):
    build_dir = args.build_dir
    port = args.port
    httpd = server(port)
    os.chdir(build_dir)
    print("serving from {0} at localhost:".format(build_dir) + str(port))
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print("\n...shutting down http server")
        httpd.shutdown()
        sys.exit()


if __name__ == "__main__":
    parser = make_parser(argparse.ArgumentParser())
    args = parser.parse_args()
    main(args)

import os
import sys
import argparse
import http.server
import socketserver
import pathlib

BUILD_PATH = pathlib.Path(__file__).resolve().parents[2] / "build"


class Handler(http.server.SimpleHTTPRequestHandler):
    def end_headers(self):
        self.send_header("Access-Control-Allow-Origin", "*")
        super().end_headers()


Handler.extensions_map[".wasm"] = "application/wasm"


def make_parser(parser):
    parser.description = "Start a server with the supplied " "build_dir and port."
    parser.add_argument(
        "--build_dir",
        action="store",
        type=str,
        default=BUILD_PATH,
        help="set the build directory",
    )
    parser.add_argument(
        "--port", action="store", type=int, default=8000, help="set the PORT number"
    )
    return parser


def server(port):
    httpd = socketserver.TCPServer(("", port), Handler)
    return httpd


def main(args):
    build_dir = args.build_dir
    port = args.port
    httpd = server(port)
    os.chdir(build_dir)
    print("serving from {0} at localhost:".format(build_dir) + str(port))
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print("\n...shutting down http server")
        httpd.shutdown()
        sys.exit()


if __name__ == "__main__":
    parser = make_parser(argparse.ArgumentParser())
    args = parser.parse_args()
    main(args)
