/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
@bug 4843136 4763384
@summary Various race conditions caused exec'ed processes to have
extra unused file descriptors, which caused hard-to-reproduce hangs.
@author Martin Buchholz
*/
public class SleepyCat {
}
private boolean timedOut;
this.timedOut = false;
}
public void run() {
timedOut = true;
}
public boolean timedOut() {
return timedOut;
}
}
// Time out was reproducible on Solaris 50% of the time;
// on Linux 80% of the time.
//
// Scenario: After fork(), parent executes and closes write end of child's stdin.
// This causes child to retain a write end of the same pipe.
// Thus the child will never see an EOF on its stdin, and will hang.
// Increasing the iteration count makes the bug more
// reproducible not only for the obvious reason, but also for
// slower, making the child more likely to win the race!
int iterations = 20;
int timeout = 30;
s.close(); // race condition here
}
if (catExecutioner.timedOut())
return catExecutioner.timedOut();
}
// Inspired by the imaginative test case for
// 4850368 (process) getInputStream() attaches to forked background processes (Linux)
// Time out was reproducible on Linux 80% of the time;
// never on Solaris because of explicit close in Solaris-specific code.
// Scenario: After fork(), the parent naturally closes the
// child's stdout write end. The child dup2's the write end
// of its stdout onto fd 1. On Linux, it fails to explicitly
// close the original fd, and because of the parent's close()
// of the fd, the child retains it. The child thus ends up
// with two copies of its stdout. Thus closing one of those
// write fds does not have the desired effect of causing an
// EOF on the parent's read end of that pipe.
int iterations = 10;
int timeout = 30;
byte[] buffer = new byte[10];
for (int i = 0;
++i) {
try {
// should get immediate EOF, but might hang
throw new Exception("Expected EOF, got a byte");
} catch (IOException e) {
// Stream closed by sleeperExecutioner
break;
}
}
if (sleeperExecutioner.timedOut())
return sleeperExecutioner.timedOut();
}
try {
throw new Exception("Read from closed pipe hangs");
} catch (IOException e) {
// We will get here on non-Posix systems,
// which don't have cat and sleep and sh.
}
}
}