Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sockets not thread safe on windows #12756

Closed
p5pRT opened this issue Feb 5, 2013 · 3 comments
Closed

sockets not thread safe on windows #12756

p5pRT opened this issue Feb 5, 2013 · 3 comments

Comments

@p5pRT
Copy link

p5pRT commented Feb 5, 2013

Migrated from rt.perl.org#116641 (status was 'new')

Searchable as RT116641$

@p5pRT
Copy link
Author

p5pRT commented Feb 5, 2013

From goossen@textkernel.nl

Created by goossen@textkernel.nl

creating and freeing sockets appears not to be thread safe on windows.
See attached test. Errors include
"An operation was attempted on something that is not a socket."
on operations on sockets are the socket has been sucessfully created.
Adding locks around the creation and closing of the sockets seem to
fix the problem.

Perl Info

Flags:
    category=core
    severity=high

Site configuration information for perl 5.16.2:

Configured by strawberry-perl at Fri Nov  2 00:34:53 2012.

Summary of my perl5 (revision 5 version 16 subversion 2) configuration:

  Platform:
    osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread
    uname='Win32 strawberry-perl 5.16.2.1 #1 Fri Nov  2 00:33:54 2012 i386'
    config_args='undef'
    hint=recommended, useposix=true, d_sigaction=undef
    useithreads=define, usemultiplicity=define
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=undef, use64bitall=undef, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='gcc', ccflags =' -s -O2 -DWIN32  -DPERL_TEXTMODE_SCRIPTS
-DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -fno-strict-aliasing
-mms-bitfields',
    optimize='-s -O2',
    cppflags='-DWIN32'
    ccversion='', gccversion='4.6.3', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='long
long', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='g++', ldflags ='-s -L"C:\strawberry\perl\lib\CORE"
-L"C:\strawberry\c\lib"'
    libpth=C:\strawberry\c\lib C:\strawberry\c\i686-w64-mingw32\lib
    libs=-lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32
-ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32
-lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32
    perllibs=-lmoldname -lkernel32 -luser32 -lgdi32 -lwinspool
-lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid
-lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32
    libc=, so=dll, useshrplib=true, libperl=libperl516.a
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
    cccdlflags=' ', lddlflags='-mdll -s
-L"C:\strawberry\perl\lib\CORE" -L"C:\strawberry\c\lib"'

Locally applied patches:



@INC for perl 5.16.2:
    C:/strawberry/perl/site/lib
    C:/strawberry/perl/vendor/lib
    C:/strawberry/perl/lib
    .


Environment for perl 5.16.2:
    HOME (unset)
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=C:\Program Files (x86)\ActiveState Perl Dev Kit
9.1.1\bin\;C:\Perl\site\bin;C:\Perl\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program
Files (x86)\SlikSvn\bin\;C:\MinGW\bin;C:\MinGW\msys\1.0\bin;C:\Borland\BCC55\Bin;C:\Program
Files (x86)\Windows Installer XML v3\bin;C:\Program Files
(x86)\Git\cmd;C:\strawberry\c\bin;C:\strawberry\perl\site\bin;C:\strawberry\perl\bin
    PERL_BADLANG (unset)
    SHELL (unset)

@p5pRT
Copy link
Author

p5pRT commented Feb 5, 2013

From goossen@textkernel.nl

#!/usr/bin/env perl

use strict;
use warnings;

use Test::More;

use threads;
use threads::shared;

use File::Spec;
use IO::Select;
use IO::Socket::INET;

plan "no_plan";

our $G_LOCK = &share( [] );
my $use_lock = 0;

my $input = "foobar\n";
my $output = "baz\n";

my $port = 11100;

threads->create( sub {
print "server\n";
my $listen = IO::Socket::INET->new( Listen => 15, Proto => "tcp", LocalAddr => "localhost", LocalPort => $port ) or die $!;

print "waiting\n";
while (1) {

my $sel = IO::Select->new($listen);
$sel->can_read() or die "Failed can_read: $!";

my $sock;
{
lock $G_LOCK if $use_lock;
$sock = $listen->accept();
}
eval {
my $line = $sock->getline();
$sock->print($output);
# sleep 0.1;
lock $G_LOCK if $use_lock;
$sock->close;
};
warn "SERVER: $@" if $@;
}
die "FAILED accept: $!";
exit;
}
);

sleep 3;

my $G_NO_OF_THREADS = 10;

for my $ground ( 1 .. 3 ) {

print STDERR "\n\nGLOBAL ROUND $ground\n\n";

my @threads;

for ( 1 .. $G_NO_OF_THREADS ) {
push @threads, threads->create( \&test_process_many );
}

for (@threads) {
is( $_->join, '', "no error in thread" );
}
}

sub test_process_many {

eval {
for my $i ( 1 .. 1000 ) {
test_process($input, $output);
}
};
warn "CLIENT: $@" if $@;
return $@;
}

sub test_process {
my ( $input, $exp_res ) = @_;

my $socket;
{
lock $G_LOCK if $use_lock;
$socket = IO::Socket::INET->new( PeerAddr => "localhost:$port" );
die "failed creating socket: $!" unless $socket;
}

$socket->print($input);

my @res = <$socket>;
my $res = join "", @res;
$res =~ s/\r\n/\n/g;
$res eq $exp_res or die "Invalid result '$res'";

{
lock $G_LOCK if $use_lock;
$socket->close();
}

return;
}

@xenu
Copy link
Member

xenu commented Mar 10, 2023

This was fixed back in 2013 with this commit: b47a847.

PS. That fix had to be disabled for Visual C++ 2015 (and newer) builds. This PR will solve that: #20922

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants