question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

proxy doesn't know if karma webserver runs on different port than originally configured

See original GitHub issue

I’m using karma to test a project loaded by systemjs. For lack of a better way, my karma config depends heavily on use of proxies to rewrite paths (but I’m proxying back to karma’s internal webserver, not a different webserver).

I’m also running these tests via a parallel test runner that runs several copies of karma. The first karma starts up on the port configured in karma.conf.js (9050 for me). The second one gets EADDRINUSE, logs “port 9050 in use”, and happily rolls onto port 9051.

The problem is that karma configures the proxy with the port specified via config (thanks to https://github.com/karma-runner/karma/pull/1007) falling back to default 443/80 if the config doesn’t specify. And karma configures the proxy library before it calls listen on its internal webserver, that is, before it discovers the port conflict and starts the webserver on a different port. And when it does discover the port conflict, it never tells the proxy library.

The result in my parallel test runner scenario is this: I start N copies of karma, they start their internal webservers on N different ports starting at 9050, but they’ve all configured their proxy to proxy to 9050. So all the proxied requests go to the webserver for the first karma instance to start. This actually works out better than you might expect, since they’re all serving the same code… until the first karma test passes and exits (the batch tests run in single-run mode), and the rest of the tests keep trying to talk to the webserver that just exited.

This is easy to repro without the parallel scenario; any port conflict will do:

  • take any karma test that has a config which proxies requests back to the internal webserver
  • netcat -lp [the port the karma config uses]
  • run the karma test

You’ll see karma itself log “port in use” and increment until it finds an open port, but continue throwing proxied requests at the originally configured port.

A brute force fix for this is to defer evaluation of the proxyConfig port until it’s actually needed, by which time karma’s listening on the right port. A cleaner fix that doesn’t rely on getters would probably have the outer glue code that starts everything wait till the internal webserver owns a port and then tell the proxy code explicitly to use that port. The initialization order and data flow is not very explicit thanks to DI so it’s not immediately obvious to me how to accomplish that.

diff --git a/node_modules/karma/lib/middleware/proxy.js b/node_modules/karma/lib/middleware/proxy.js
index e439c52..9485965 100644
--- a/node_modules/karma/lib/middleware/proxy.js
+++ b/node_modules/karma/lib/middleware/proxy.js
@@ -45,7 +45,12 @@ var parseProxyConfig = function(proxies, config) {
     if (!proxyConfig[proxyPath].port) {
       if (!proxyConfig[proxyPath].host) {
         proxyConfig[proxyPath].host = config.hostname;
-        proxyConfig[proxyPath].port = config.port;
+        Object.defineProperty(proxyConfig[proxyPath], 'port', {
+          get: function() {
+            return config.port;
+         },
+        });
       } else {
         proxyConfig[proxyPath].port = proxyConfig[proxyPath].https ? '443' : '80';
       }

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:7
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
leoseligcommented, Apr 7, 2016

Also experienced this just now, took some time to figure out what went wrong, hence my follow-up question: is it possible to tell karma to fail if the port is in use?

0reactions
raspocommented, Jan 30, 2018

I encountered this bug when running tests concurrently (using different instances of Karma).

An easy way to reproduce it is to launch your instance of karma (one that uses proxies). In a different terminal window launch another identical instance. The second instance will notice that [karma]: Port 9876 in use and use the next available port. So far so good. Now kill the first instance. This will interrupt the proxy on port 9876. The second one will start throwing 404 errors.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trouble configuring karma proxy at website root
My issue was two-fold: I didn't understand the individual parts of the proxy object, and I had a mistake in the link I...
Read more >
Configuration File - Karma
Description: When Karma is watching the files for changes, it tries to batch multiple changes into a single run so that the test...
Read more >
Angular: Karma Proxy Configuration
The way that Karma works is that it spins up it's own web server on a pre-configured port, found in the karma.config.js file...
Read more >
978782 – firefox 22 tries to use user configured proxy even if ...
Check the proxy settings to make sure that they are correct. Contact your network administrator to make sure the proxy server is working....
Read more >
Test Configuration Options
When setting up your test, you'll need to configure your script with settings called capabilities that align with your test environment (e.g., ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found