Skip to content

XMLHttpRequest fails when called more than once #475

Open
@odrail

Description

@odrail

Issue type

Bug

How did you install PythonMonkey?

Installed from pip

OS platform and distribution

Windows 11

Python version (python --version)

3.12

PythonMonkey version (pip show pythonmonkey)

1.1.0

Bug Description

The first time I call the clientHttp function, it works fine, but when I call it more than once, I get the following error:

  error: NetworkError: Error: Python AttributeError: 'NoneType' object has no attribute 'connect'
    DOMException@C:/Users/emanu/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0/LocalCache/local-packages/Python312/site-packages/pythonmonkey/node_modules/core-js/modules/web.dom-exception.constructor.js:64:17
    #handleErrors@C:/Users/emanu/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0/LocalCache/local-packages/Python312/site-packages/pythonmonkey/builtin_modules/XMLHttpRequest.js:452:48
    C_Users_emanu_AppData_Local_Packages_PythonSoftwareFoundation_Python_3_12_qbz5n2kfra8p0_LocalCache_local_packages_Python312_site_packages_pythonmonkey_builtin_modules_XMLHttpRequest_js/#sendAsync/<@C:/Users/emanu/AppData/Local/Packages/PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0/LocalCache/local-packages/Python312/site-packages/pythonmonkey/builtin_modules/XMLHttpRequest.js:429:38,
function clientHttp(url, options = {}) {
    return new Promise((resolve, reject) => {
        const { method = 'GET' } = options
        const xhr = new XMLHttpRequest()
        xhr.open(method, url, true)

        setHeaders(options.headers, xhr)

        const token = read('token.txt')
        setAuthorization(xhr, token)

        xhr.onerror = (e) => {
            console.log('xhr error:', e)
            reject(xhr.statusText)
        }
        
        xhr.onload = () => {
            if (xhr.status >= 200 && xhr.status < 300) {
                return resolve(parseBody(xhr))
            }
            return reject(parseBody(xhr))
        };

        xhr.send(options.body)
    })
}

Standalone code to reproduce the issue

Relevant log output or backtrace

Additional info if applicable

What branch of PythonMonkey were you developing on? (If applicable)

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions