JDK-4392953 : SourceDataLine.drain() hangs in threaded use
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.sound
  • Affected Version: 1.3.0,1.4.0
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: solaris_2.5.1,windows_nt
  • CPU: x86,sparc
  • Submitted: 2000-11-28
  • Updated: 2003-10-24
  • Resolved: 2003-08-25
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
1.4.2 1.4.2Fixed
Related Reports
Duplicate :  
Relates :  
Description

Name: yyT116575			Date: 11/28/2000


bash-2.02$ java -version
java version "1.3.0"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0-C)
Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)

Run the following code.  The "ringin.wav" that it accesses should be the one
found on Windows NT under c:\Program Files\PhoneTools (an 18K file), not the 10K
version that appears elsewhere.  (I have seen this same problem occur with other
.wav files as well, but that particular one seems to produce it quickly and
reliably.)

Typically after about 10 rings, one or both threads hang in the drain method.
If this doesn't seem to be happening on your machine, please try with other .wav
files and/or with other Thread.sleep values in MyThread.run.

Of course drain may be an innocent bystander rather than the actual culprit.



import java.net.*;
import javax.sound.sampled.*;

class Test0
{
    public static void main(String[] args)
    {
	try {
	    go();
	}
	catch (Exception ex)
	    {
		ex.printStackTrace();
	    }
    }

    private static void go() throws Exception
    {
 	URL u5 = new URL("file:ringin.wav");

	MyThread t1 = new MyThread(u5);
	t1.start();

	while (true)
	    {
		showUs(u5);
	    }
    }

    private static class MyThread extends Thread
    {
	URL u;
	MyThread(URL u)
	{
	    this.u = u;
	}
	
	public void run(){
	    while (true)
		{
		    try {
			Thread.sleep(1000);
		    } catch (InterruptedException ex) {}
		    
		    try {
			showUs(u);
		    } catch (Exception ex)
			{
			    System.out.println("From MyThread:");
			    ex.printStackTrace();
			}
		}
	}
    }

    private static int count = 0;

    private static void showUs(URL u) throws Exception
    {
	System.out.println();

	count++;
	int mycount = count;
	System.out.println("Trial number " + count);
	System.out.println(u);
	AudioFileFormat fformat =
	    AudioSystem.getAudioFileFormat(u);
	
	AudioInputStream astream = AudioSystem.getAudioInputStream(u);
	AudioFormat format = astream.getFormat();
	System.out.println(format);
	
	DataLine.Info sourceInfo =
	    new DataLine.Info(SourceDataLine.class, format);
	System.out.println(sourceInfo);
	SourceDataLine line = (SourceDataLine)AudioSystem.getLine(sourceInfo);
	line.open(format);
	line.start();

	int fsize = format.getFrameSize();
	byte[] buf = new byte[fsize];
	while ( true )
	    {
		int offset = 0;
		int toRead = fsize;
		int len = 0;
		while ( toRead > 0 )
		    {
			len = astream.read(buf, offset, toRead);
			if ( len == 0 )
			    System.out.println("Read zero bytes");
			if ( len == -1 )
			    break;
			offset += len;
			toRead -= len;
		    }
		if ( len == -1 )
		    break;
		line.write(buf, 0, fsize);
	    }
	astream.close();
	System.out.println("Draining " + mycount);
	line.drain();
	System.out.println("finished draining " + mycount);
	line.stop();
	line.close();
    }
}
(Review ID: 112901) 
======================================================================

Comments
CONVERTED DATA BugTraq+ Release Management Values COMMIT TO FIX: 1.4.2 tiger-beta FIXED IN: 1.4.2 tiger-beta INTEGRATED IN: 1.4.2 tiger-b26 tiger-beta
14-06-2004

WORK AROUND Name: yyT116575 Date: 11/28/2000 A spin/sleep loop on line.isActive() seems like a reasonable proxy but also seems not to be an exact proxy. If all you want to do is wait until the line is for-sure empty you can do a sleep based on its buffer length but of course that can be too long. ======================================================================
11-06-2004

PUBLIC COMMENTS SourceDataLine.drain() hangs in threaded use
10-06-2004

EVALUATION ###@###.### 2001-07-06 I'm not able to reproduce this, but I've had problems with drain, too. Needs more investigation. Unable to reproduce the problem. Will be investigated more. ###@###.### 2002-10-30 On Windows 2000, I could reproduce it with 1.3.0 after 14 iterations, with 1.4.1 after 1000 iterations. Solaris with 1.4.2 build 04 produced a crash after about 200 iterations in realloc(). I leave this bug open to verify its proper fix with the upcming direct audio renderers in Tiger (and 1.4.2 for Linux). ###@###.### 2003-08-24 Reproducible on Solaris 1.3.0-1.4.1, but not with mantis or tiger b16. Ran tiger b16 for 24 hours (>100000 iterations) without problems. Close as fixed in 1.4.2.
24-08-2003