Potential socket issue in LC 7.0.6

If you find an issue in LiveCode but are having difficulty pinning down a reliable recipe or want to sanity-check your findings with others, this is the place.

Please have one thread per issue, and try to summarize the issue concisely in the thread title so others can find related issues here.

Moderator: Klaus

Post Reply
russellf
Posts: 17
Joined: Thu Feb 11, 2016 8:34 pm

Potential socket issue in LC 7.0.6

Post by russellf » Fri Feb 12, 2016 5:45 pm

So I've created a server that talks to a bunch of client processes using sockets. Problem is, it seems some of the sockets are reused before they are closed. Is this expected? I've attached a screenshot of the command window after running the openSockets command on my server. You can see the duplicate entries for '127.0.0.1:1404'. Should this occur? If not, I will attempt to ween down my code to something that can be added to a bug description. Thanks!

Edit: This is on Windows 7
Attachments
openSockets.png
Screenshot

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Potential socket issue in LC 7.0.6

Post by FourthWorld » Fri Feb 12, 2016 7:05 pm

Could it be that's a client with two transactions? Without seeing the code of won't be possible to determine what's going on.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

russellf
Posts: 17
Joined: Thu Feb 11, 2016 8:34 pm

Re: Potential socket issue in LC 7.0.6

Post by russellf » Fri Feb 12, 2016 7:16 pm

That's the server side. These are all open sockets created from the accept connections command, in this case:

accept connections on port 61746 with message XXX

The server does not open any sockets any other way. In this particular instance there's no clear way to specify a client in the read to/write from socket commands since both clients connect to the same socket. If you believe this shouldn't occur then I can try to create a smaller test case outside this code to recreate, but it would take some time and I didn't want to do that if I'm thinking of it all incorrectly. Here's the overview of what I am doing within a larger project:

1. Create a server using accept connection on port X.
2. Spawn 20 client processes that connect to the server and receive a task to perform.
3. Task clients disconnect when complete.

The command in the screenshot should illustrate the problem I'm seeing, since even if there were other connections to the same socket wouldn't they have the "|Connection ID" associated with them? Either way I'm sure the server only accepts connections. Thanks for the quick feedback!

russellf
Posts: 17
Joined: Thu Feb 11, 2016 8:34 pm

Re: Potential socket issue in LC 7.0.6

Post by russellf » Tue Feb 23, 2016 2:20 pm

No updates? Perhaps someone could answer this question:

Should livecode ever have duplicate entries in the openSockets list?

I would think not, since these describe the peer connections, which should always be unique. Otherwise, how could you know who you were communicating with using read from/write to socket?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Potential socket issue in LC 7.0.6

Post by FourthWorld » Tue Feb 23, 2016 3:58 pm

Many things are possible, depending on what the code is doing. Without seeing both the client and server code, we may not be able to guess how that seemingly-duplicate entry occurred.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

russellf
Posts: 17
Joined: Thu Feb 11, 2016 8:34 pm

Re: Potential socket issue in LC 7.0.6

Post by russellf » Tue Feb 23, 2016 4:50 pm

It seems rather odd that there is no definitive answer on whether the livecode function openSockets should have duplicates or not. I will gladly pare down my code to something I can post to the forums in the meantime, but it would be nice to know if this should even occur. I asure you nothing nefarious is going on, as in I'm not altering the output of this function in any way, shape, or form.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Potential socket issue in LC 7.0.6

Post by FourthWorld » Tue Feb 23, 2016 9:32 pm

russellf wrote:...it would be nice to know if this should even occur.
Computers are generally deterministic systems; given the same inputs in the same order will yield the same result. Sometimes sockets are closed after a single use, sometimes used more than once. How they're used in the code in question can only be determined by the code. So whether this should happen or not can't be known right now, but once we see the code for a repeatable example we'll be able to pin down what's going on.

One side note occurs to me as I read the thread title - are you still testing on v7.0.6 or have you tried a more recent version? The most recent Stable build in the 7.x series is 7.1.1, but for this sort of testing I'd suggest the latest Release Candidate for 7.1.2:
http://downloads.livecode.com/livecode/

I believe some work has been done in areas that may affect sockets, so while (if memory serves) that work was focused on the Server edition it may affect other platforms as well. Maybe it was a bug that was fixed? Maybe it's fine and doing exactly what it should? We'll find out with a little more testing and perhaps some code review.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

russellf
Posts: 17
Joined: Thu Feb 11, 2016 8:34 pm

Re: Potential socket issue in LC 7.0.6

Post by russellf » Tue Feb 23, 2016 10:06 pm

Yes, computers are deterministic, therefore the livecode openSockets function was designed with an intent. Was the intent to allow duplicate entries? Anyway, I'll get some code to review, but it will be a couple of weeks. I've worked around the issue by using another list to track the sockets in use.

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10043
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Potential socket issue in LC 7.0.6

Post by FourthWorld » Tue Feb 23, 2016 10:47 pm

The sockets IDs come from the OS, and if I read your last post right it seems the fix was in changing the method by which they're listed. Doesn't sound like anything LC is doing at all, but to answer your question "Was the intent to allow duplicate entries?", the answer would be "Depends on what the code is asking it to do."
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

russellf
Posts: 17
Joined: Thu Feb 11, 2016 8:34 pm

Re: Potential socket issue in LC 7.0.6

Post by russellf » Tue Feb 23, 2016 10:57 pm

Looking through the livecode open source project some, the openSockets function is a representation of MCsockets. To read/write a socket, the code looks up the connection id given by a user in MCsockets, say "127.0.0.1:4400" and finds the first entry in the list that matches it. It uses this descriptor to talk to the socket. So it looks to me like the name field in this list should be unique, otherwise which socket would you be talking to?

LCMark
Livecode Staff Member
Livecode Staff Member
Posts: 1232
Joined: Thu Apr 11, 2013 11:27 am

Re: Potential socket issue in LC 7.0.6

Post by LCMark » Thu Mar 03, 2016 12:32 pm

@russellf: You are correct - the openSockets command *should not* produce a list containing duplicate names as those names are used to *uniquely* identify a socket. It does sound like the OS is closing a socket, but LiveCode is not updating its list so that when that socket's port is reused in a subsequent connection you end up with two sockets with the same name. I'm honestly not sure why this is occurring - it is quite possibly a bug caused by a specific sequence of actions your application is taking. If you could file a bug report at http://quality.livecode.com with as much information as possible and ideally a stack reproducing the problem that would be great (if you aren't able to produce a cut down example stack, you can send your actual stack which demonstrates the problem to panos.merakos@livecode.com - just put a note to that effect in the bug report).

russellf
Posts: 17
Joined: Thu Feb 11, 2016 8:34 pm

Re: Potential socket issue in LC 7.0.6

Post by russellf » Fri Mar 04, 2016 2:38 pm

@LCMark: Thanks for the reply! Good to know I'm not crazy :) I will do as you say but it may be some time before I can get this together. Need to get my project released in the next week or two. Thanks again!

Post Reply