No OpenGL context found with new game sessions.

Hi,
I’m doing a game with libGDX. As it is multiplayer, I need to launch the client multiple times for testing purposes (I use debug mode with eclipse).
When I start a second game session, it crashes, letting me this message:

[quote]java.lang.RuntimeException: No OpenGL context found in the current thread.
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
at org.lwjgl.opengl.GL11.glGenTextures(GL11.java:1403)
at com.badlogic.gdx.backends.lwjgl.LwjglGL20.glGenTexture(LwjglGL20.java:348)
at com.badlogic.gdx.graphics.Texture.(Texture.java:120)
at com.badlogic.gdx.graphics.Texture.(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.(Texture.java:92)
at com.badlogic.gdx.graphics.Texture.(Texture.java:88)
at model.haraka.be.Warrior.loadTextures(Warrior.java:52)
at network.haraka.be.RequestHandler.addPlayer(RequestHandler.java:161)
at network.haraka.be.RequestHandler.parseRequest(RequestHandler.java:37)
at network.haraka.be.ClientSession.run(ClientSession.java:55)
at java.lang.Thread.run(Thread.java:748)
[/quote]
I saw some post saying this is a problem between threads and openGL, but since my OpenGL and libGDX knowledge is little I didn’t understand a lot.

How can I fix that ? Can you explain a newbie the problem?

Thank you :slight_smile:

Hey, I’m not an OpenGL expert but I’d say that this problem is caused by using multiple threads. You have a thread in your system which tries to use an OpenGL context resource but it has no access to it due to the fact that this resource is located to a different thread. Are you using kryonet as networking library? I was encountering this problem as well I had to make sure that I use the right threads at the right time and do not mix things up.

Thanks for the reply,
I’m using a second thread for the network, it’s using socket.io and it makes action on the model (rendered in the first thread).
What do you mean by using the right thread at the right time ? Can you give me an example ?

It seems to me that you are creating the texture on the networking thread, where OpenGL context is not current. I think you should be creating the thread in the main thread. Maybe make request handler queue the requests and you process that request queue in your game’s update operation.

So you think my network thread should store the requestes and only the main thread should ask and act on these requestes one by one ?
This should take time to change, so I wanna make sure :slight_smile:

From my opinion you do not have to create a Requesthandler necessarily. Maybe you can just switch threads before your operation or rearrange your code in a way that openGL operations are only accessed from the openGL thread.

I also recommend you to take a look at this since it provides some information regarding this topic:

http://www.java-gaming.org/index.php?topic=21001.0

Problem solved.

Simply managed to make my network thread register all requests in an ArrayList and my Main thread take care of every requests and clear the ArrayList on every game refresh.
Didn’t have to change anything in my Server.

Thank you everyone, you helped !

ArrayList is not synchronized… what you probably want is maybe a LinkedBlockingQueue, where the main OpenGL thread uses queue.drainTo(thingsToDo) to get all the new tasks it needs to do, and the network thread uses queue.add(thingToDo). This will avoid the inevitable curious explosion when both threads try to read and write from the ArrayList at the same time leaving you with an inexplicable, hard-to-reproduce bug at completely random times…

Cas :slight_smile:

Thank you!
Can you check if this is correct ?

Network Thread:

private BlockingQueue<String> requestes = new ArrayBlockingQueue<String>(150);

	@Override
	public void run() {
		while(true) {
			try {
				String request = br.readLine();
				System.out.println("[Client Session] Request received from server: "+request);
				requestes.put(request);
			} catch (IOException e) {
				e.printStackTrace();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}

OpenGL/Main Thread:


	@Override
	public void render(float delta) {
		while (clientSession.getRequestes().size() > 0) {
			try {
				clientSession.getRh().parseRequest(clientSession.getRequestes().take().split(" "));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		
		//Rendering the game
		rm.render(delta);

	}

I use String to make the server and the game communicate.

Don’t use size() like that, it’s “slow” - do this instead, which reads it once, then drains the whole lot to an intermediary list:


	int size = clientSession.getRequests().size();
	if (size > 0) {
		List<String> requests = new ArrayList<>(size);
		clientSession.getRequests().drainTo(requests);
		requests.forEach(request -> clientSession.getRh().parseRequest(request.split(" ")));
	}

Cas :slight_smile: