[Pharo-project] Socket objects created twice ?
Hi All, I encountered something that I do not understand. I tried this in 1.2.2 and 1.4 with both my normal, older CogVM as well as the lastest one by Estaban. In a normal clean image, there would be not Socket instances. Socket allInstances size = 0 Now create any kind of Socket Socket new (or newTCP, newUDP) Now Socket allInstances size = 2 So Smalltalk reports two instances, but it seems they share the same socketHandle and semaphores, so one seems to be a shallow copy of the other. (Close the one instance, 3 timesRepeat: [ Smalltalk garbageCollect ] and they are both gone). Is this normal ? Is there really only one OS level socket ? Does this have to do with Sockets being in a WeakRegistry ? It is confusing to say the least. Sven
I have seen this too (1.1.1), and it bothered me. I *think* you will find that the handles of the two sockets are the same, but it "Just ain't right" (TM). Bill ________________________________________ From: pharo-project-bounces@lists.gforge.inria.fr [pharo-project-bounces@lists.gforge.inria.fr] On Behalf Of Sven Van Caekenberghe [sven@beta9.be] Sent: Tuesday, July 12, 2011 7:38 AM To: An open mailing list to discuss any topics related to an open-source Smalltalk Subject: [Pharo-project] Socket objects created twice ? Hi All, I encountered something that I do not understand. I tried this in 1.2.2 and 1.4 with both my normal, older CogVM as well as the lastest one by Estaban. In a normal clean image, there would be not Socket instances. Socket allInstances size = 0 Now create any kind of Socket Socket new (or newTCP, newUDP) Now Socket allInstances size = 2 So Smalltalk reports two instances, but it seems they share the same socketHandle and semaphores, so one seems to be a shallow copy of the other. (Close the one instance, 3 timesRepeat: [ Smalltalk garbageCollect ] and they are both gone). Is this normal ? Is there really only one OS level socket ? Does this have to do with Sockets being in a WeakRegistry ? It is confusing to say the least. Sven
On 12.07.2011 13:38, Sven Van Caekenberghe wrote:
Hi All,
I encountered something that I do not understand. I tried this in 1.2.2 and 1.4 with both my normal, older CogVM as well as the lastest one by Estaban.
In a normal clean image, there would be not Socket instances.
Socket allInstances size = 0
Now create any kind of Socket
Socket new (or newTCP, newUDP)
Now
Socket allInstances size = 2
So Smalltalk reports two instances, but it seems they share the same socketHandle and semaphores, so one seems to be a shallow copy of the other.
(Close the one instance, 3 timesRepeat: [ Smalltalk garbageCollect ] and they are both gone).
Is this normal ? Is there really only one OS level socket ? Does this have to do with Sockets being in a WeakRegistry ?
It is confusing to say the least.
Sven Yes it's normal, one is the finalizer of the other.
To differentiate them more easily, you could do Socket >> actAsFinalizer socketHandle := socketHandle bitInvert. and finalize self primSocketDestroyGently: socketHandle bitInvert. This will (99.9% likely at least) give a printstring of invalidSocketHandle for the finalizing Socket instance. Cheers, Henry
Rather than changing the handle (making it harder to associate the handle with the "real" socket), it would be nicer to have a SocketFinalizer or something - it could even be a subclass of socket?? ________________________________________ From: pharo-project-bounces@lists.gforge.inria.fr [pharo-project-bounces@lists.gforge.inria.fr] On Behalf Of Henrik Sperre Johansen [henrik.s.johansen@veloxit.no] Sent: Tuesday, July 12, 2011 8:16 AM To: Pharo-project@lists.gforge.inria.fr Subject: Re: [Pharo-project] Socket objects created twice ? On 12.07.2011 13:38, Sven Van Caekenberghe wrote:
Hi All,
I encountered something that I do not understand. I tried this in 1.2.2 and 1.4 with both my normal, older CogVM as well as the lastest one by Estaban.
In a normal clean image, there would be not Socket instances.
Socket allInstances size = 0
Now create any kind of Socket
Socket new (or newTCP, newUDP)
Now
Socket allInstances size = 2
So Smalltalk reports two instances, but it seems they share the same socketHandle and semaphores, so one seems to be a shallow copy of the other.
(Close the one instance, 3 timesRepeat: [ Smalltalk garbageCollect ] and they are both gone).
Is this normal ? Is there really only one OS level socket ? Does this have to do with Sockets being in a WeakRegistry ?
It is confusing to say the least.
Sven Yes it's normal, one is the finalizer of the other.
To differentiate them more easily, you could do Socket >> actAsFinalizer socketHandle := socketHandle bitInvert. and finalize self primSocketDestroyGently: socketHandle bitInvert. This will (99.9% likely at least) give a printstring of invalidSocketHandle for the finalizing Socket instance. Cheers, Henry
On 12 July 2011 15:17, Schwab,Wilhelm K <bschwab@anest.ufl.edu> wrote:
Rather than changing the handle (making it harder to associate the handle with the "real" socket), it would be nicer to have a SocketFinalizer or something - it could even be a subclass of socket??
I just thought about it after posting previous mail. Yes it would be nice to add an explicit class, named SocketFinalizer and then add an inst var to Socket - a finalizer. Then when you deleting a socket, a socket could tell a finalizer to nil-out handle, instead of asking WeakRegistry to unregister the socket which is much slower. And a finalization action for nilled out handle will be no-op, once already closed socket become garbage. In that way, someday we could eliminate the #remove: protocol from WeakRegistry. -- Best regards, Igor Stasenko AKA sig.
Sig, Sounds good, with the possible exception of removing #remove:. Letting things happen is great, and giving the finalizer a recognizable name would be wonderful. Otherwise, one should be able to do explicit cleanup if desired[*]. Whether that applies to WeakRegistry, I'm not sure. I would want to be able to remove items from weak arrays, dictionaries, etc. [*] I am thinking of things like tight loops with database activity, etc. There have been times when I have had to explicitly clean up finaizable things, and cleaning the weak registrations at the same time always seemed reasonable. Bill ________________________________________ From: pharo-project-bounces@lists.gforge.inria.fr [pharo-project-bounces@lists.gforge.inria.fr] On Behalf Of Igor Stasenko [siguctua@gmail.com] Sent: Tuesday, July 12, 2011 10:13 AM To: Pharo-project@lists.gforge.inria.fr Subject: Re: [Pharo-project] Socket objects created twice ? On 12 July 2011 15:17, Schwab,Wilhelm K <bschwab@anest.ufl.edu> wrote:
Rather than changing the handle (making it harder to associate the handle with the "real" socket), it would be nicer to have a SocketFinalizer or something - it could even be a subclass of socket??
I just thought about it after posting previous mail. Yes it would be nice to add an explicit class, named SocketFinalizer and then add an inst var to Socket - a finalizer. Then when you deleting a socket, a socket could tell a finalizer to nil-out handle, instead of asking WeakRegistry to unregister the socket which is much slower. And a finalization action for nilled out handle will be no-op, once already closed socket become garbage. In that way, someday we could eliminate the #remove: protocol from WeakRegistry. -- Best regards, Igor Stasenko AKA sig.
On 12 July 2011 20:53, Schwab,Wilhelm K <bschwab@anest.ufl.edu> wrote:
Sig,
Sounds good, with the possible exception of removing #remove:. Â Letting things happen is great, and giving the finalizer a recognizable name would be wonderful. Â Otherwise, one should be able to do explicit cleanup if desired[*]. Â Whether that applies to WeakRegistry, I'm not sure. Â I would want to be able to remove items from weak arrays, dictionaries, etc.
it only applies to weak registry.
[*] I am thinking of things like tight loops with database activity, etc. Â There have been times when I have had to explicitly clean up finaizable things, and cleaning the weak registrations at the same time always seemed reasonable.
yes, unless cleaning takes much more time than adding. Accessing weak registry is synchronized through semaphore. And for servers, where you expect to have high load on your image, the less critical sections you locking, the better.
Bill
________________________________________ From: pharo-project-bounces@lists.gforge.inria.fr [pharo-project-bounces@lists.gforge.inria.fr] On Behalf Of Igor Stasenko [siguctua@gmail.com] Sent: Tuesday, July 12, 2011 10:13 AM To: Pharo-project@lists.gforge.inria.fr Subject: Re: [Pharo-project] Socket objects created twice ?
On 12 July 2011 15:17, Schwab,Wilhelm K <bschwab@anest.ufl.edu> wrote:
Rather than changing the handle (making it harder to associate the handle with the "real" socket), it would be nicer to have a SocketFinalizer or something - it could even be a subclass of socket??
I just thought about it after posting previous mail. Yes it would be nice to add an explicit class, named SocketFinalizer and then add an inst var to Socket - a finalizer. Then when you deleting a socket, a socket could tell a finalizer to nil-out handle, instead of asking WeakRegistry to unregister the socket which is much slower. And a finalization action for nilled out handle will be no-op, once already closed socket become garbage.
In that way, someday we could eliminate the #remove: protocol from WeakRegistry.
-- Best regards, Igor Stasenko AKA sig.
-- Best regards, Igor Stasenko AKA sig.
Didn't really answer these, if it weren't obvious: On 12.07.2011 13:38, Sven Van Caekenberghe wrote:
Is there really only one OS level socket ? Yes, only the one returned from newXYZ gets registered externally. Does this have to do with Sockets being in a WeakRegistry ?
Yes, the other is used to release the externally registered resource when the one you created is no longer referenced (ie. to finalize it) Cheers, Henry
As Henrik says, it is normal: for each real socket there two Socket instances. On 12 July 2011 14:20, Henrik Sperre Johansen <henrik.s.johansen@veloxit.no> wrote:
Didn't really answer these, if it weren't obvious: On 12.07.2011 13:38, Sven Van Caekenberghe wrote:
Is there really only one OS level socket ?
Yes, only the one returned from newXYZ gets registered externally.
Does this have to do with Sockets being in a WeakRegistry ?
Yes, the other is used to release the externally registered resource when the one you created is no longer referenced (ie. to finalize it)
Cheers, Henry
-- Best regards, Igor Stasenko AKA sig.
OK thanks, Henry. This behavior is then to be expected for all objects registered for finalization I guess. On 12 Jul 2011, at 14:20, Henrik Sperre Johansen wrote:
Didn't really answer these, if it weren't obvious: On 12.07.2011 13:38, Sven Van Caekenberghe wrote:
Is there really only one OS level socket ? Yes, only the one returned from newXYZ gets registered externally. Does this have to do with Sockets being in a WeakRegistry ?
Yes, the other is used to release the externally registered resource when the one you created is no longer referenced (ie. to finalize it)
Cheers, Henry
On 12.07.2011 14:54, Sven Van Caekenberghe wrote:
OK thanks, Henry.
This behavior is then to be expected for all objects registered for finalization I guess. If using the defaults, then yes. It's not a general requirement though, you can specify whichever executor you want by using WeakRegistry#add:executor: to register , change the executor used in #add: by reimplementing #finalizer, etc.
Cheers, Henry
participants (4)
-
Henrik Sperre Johansen -
Igor Stasenko -
Schwab,Wilhelm K -
Sven Van Caekenberghe