GeezerVison is showing github.com — fix: fix race condition in dynamic namespaces · socketio/socket.io@05e1278
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/client.js -1Lines changed: 0 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -68,7 +68,6 @@ Client.prototype.connect = function(name, query){ | 68 | 68 |
| 69 | 69 | this.server.checkNamespace(name, query, (dynamicNsp) => { | 70 | 70 | if (dynamicNsp) { | 71 | | - debug('dynamic namespace %s was created', dynamicNsp.name); | 72 | 71 | this.doConnect(name, query); | 73 | 72 | } else { | 74 | 73 | debug('creation of namespace %s was denied', name); | |
Collapse file Copy file name to clipboardExpand all lines: lib/index.js +10-4Lines changed: 10 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -182,11 +182,17 @@ Server.prototype.checkNamespace = function(name, query, fn){ | 182 | 182 | return fn(false); | 183 | 183 | } | 184 | 184 | nextFn.value(name, query, (err, allow) => { | 185 | | - if (err || !allow) { | 186 | | - run(); | 187 | | - } else { | 188 | | - fn(this.parentNsps.get(nextFn.value).createChild(name)); | | 185 | + if (err || !allow) { | | 186 | + return run(); | 189 | 187 | } | | 188 | + if (this.nsps[name]) { | | 189 | + // the namespace was created in the meantime | | 190 | + debug("dynamic namespace %s already exists", name); | | 191 | + return fn(this.nsps[name]); | | 192 | + } | | 193 | + const namespace = this.parentNsps.get(nextFn.value).createChild(name); | | 194 | + debug("dynamic namespace %s was created", name); | | 195 | + fn(namespace); | 190 | 196 | }); | 191 | 197 | }; | 192 | 198 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/socket.io.js +36Lines changed: 36 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -945,6 +945,42 @@ describe('socket.io', function(){ | 945 | 945 | }); | 946 | 946 | }); | 947 | 947 | }); | | 948 | + | | 949 | + it("should handle race conditions with dynamic namespaces (#4136)", (done) => { | | 950 | + const srv = http(); | | 951 | + const sio = io(srv); | | 952 | + const counters = { | | 953 | + connected: 0, | | 954 | + created: 0, | | 955 | + events: 0, | | 956 | + }; | | 957 | + const buffer = []; | | 958 | + srv.listen(() => { | | 959 | + const handler = () => { | | 960 | + if (++counters.events === 2) { | | 961 | + done(); | | 962 | + } | | 963 | + }; | | 964 | + | | 965 | + sio | | 966 | + .of((name, query, next) => { | | 967 | + buffer.push(next); | | 968 | + if (buffer.length === 2) { | | 969 | + buffer.forEach((next) => next(null, true)); | | 970 | + } | | 971 | + }) | | 972 | + .on("connection", (socket) => { | | 973 | + if (++counters.connected === 2) { | | 974 | + sio.of("/dynamic-101").emit("message"); | | 975 | + } | | 976 | + }); | | 977 | + | | 978 | + let one = client(srv, "/dynamic-101"); | | 979 | + let two = client(srv, "/dynamic-101"); | | 980 | + one.on("message", handler); | | 981 | + two.on("message", handler); | | 982 | + }); | | 983 | + }); | 948 | 984 | }); | 949 | 985 | }); | 950 | 986 |
| |
|