JDK-4914667 : Closing and reopening MIDI IN device on Linux throws MidiUnavailableException
  • Type: Bug
  • Component: client-libs
  • Sub-Component: javax.sound
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: linux
  • CPU: x86
  • Submitted: 2003-08-29
  • Updated: 2003-10-13
  • Resolved: 2003-10-13
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
5.0 b24Fixed
Description
Using the following program, after a variable numer of iterarions, a MidiUnavailableException is thrown on the call to [MidiDevice].open().

If the try-catch block is uncommented (program sleeping for 2 seconds before the next open() call), no exception is thrown on 100 iterations.

The same problem doesn't appear at all for MIDI OUT devices.

exception:
--------
Exception in thread "main" javax.sound.midi.MidiUnavailableException: Device or resource busy
	at com.sun.media.sound.MidiInDevice.nOpen(Native Method)
	at com.sun.media.sound.MidiInDevice.implOpen(MidiInDevice.java:45)
	at com.sun.media.sound.AbstractMidiDevice.doOpen(AbstractMidiDevice.java:116)
	at com.sun.media.sound.AbstractMidiDevice.open(AbstractMidiDevice.java:88)
	at OpenClose2.main(OpenClose2.java:44)
--------

program:
--------
/*
 * OpenClose.java
 */

import javax.sound.midi.*;


/*
  run:
  java OpenClose2 100 in      for 100 iterations on the MIDI IN device
  java OpenClose2 16 out      for 16 iterations on the MIDI OUT device
*/
public class OpenClose2 {

    public static void main(String[] args) throws Exception {
	int numIterations = Integer.parseInt(args[0]);
	MidiDevice.Info[] infos = MidiSystem.getMidiDeviceInfo();
	MidiDevice outDevice = null;
	MidiDevice inDevice = null;
	out("available devices:");
	for (int i = 0; i < infos.length; i++) {
	    out(infos[i].toString());
	    MidiDevice device = MidiSystem.getMidiDevice(infos[i]);
	    if (! (device instanceof Synthesizer)) {
		if (device.getMaxReceivers() != 0) {
		    outDevice = device;
		}
		if (device.getMaxTransmitters() != 0) {
		    inDevice = device;
		}
	    }
	}
	out("Using MIDI OUT Device: " + outDevice);
	out("Using MIDI IN Device: " + inDevice);

	MidiDevice testDevice = null;
	if (args[1].equals("in")) {
	    testDevice = inDevice;
	} else {
	    testDevice = outDevice;
	}
	for (int i = 0; i < numIterations; i++) {
	    out("@@@ ITERATION: " + i);
	    testDevice.open();
	    testDevice.close();

	    // with the following code uncommented, the program runs slowly,
	    // but without errors.

// 	    try {
// 		Thread.sleep(2000);
// 	    } catch (InterruptedException e) {
// 	    }

	}
    }


    private static void out(String message) {
	System.out.println(message);
    }
}


/*** OpenClose2.java ***/
--------

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

EVALUATION ###@###.### 2003-08-28 Maybe it has to do with the thread running inside the MidiInDevice. This is supported by the fact that a delay (allowing the thread to exit after the device is closed) seems to circumvent the bug. Furthermore, MidiOutDevice has no thread and has no such problem.
11-06-2004

WORK AROUND Do not close Midi In devices and Transmitters while the programming is running.
11-06-2004

PUBLIC COMMENTS Closing and reopening MIDI IN device on Linux throws MidiUnavailableException
10-06-2004