RSelenium: server signals port is already in use
Asked Answered
E

7

28

I'm using the following code in RSelenium to open a browser. After I close the browser, or even close the handler by running remDr$close(), the port is still in use. I have to go to the terminal and manually kill the process so that the same port becomes available. Is there any automated way such that RSelenium makes the port free after it finishes scraping?

So here is my code:

library(RSelenium)
rD <- rsDriver(verbose = FALSE,port=4444L)
remDr <- rD$client
remDr$close()

Thanks

Ellipticity answered 16/5, 2017 at 2:16 Comment(0)
S
26

The process is composed of two parts a server (the Selenium Server) and a client (the browser you initiate). The close method of the remoteDriver class closes the client (the browser). The server also needs to be stopped when you are finished.

To stop the server when you are finished:

library(RSelenium)
rD <- rsDriver(verbose = FALSE,port=4444L)
remDr <- rD$client
remDr$close()

Now either explicitly stop the server:

rD$server$stop()

or if the rD object is removed the server will be stopped upon garbage collection:

library(RSelenium)
rD <- rsDriver(verbose = FALSE,port=4444L)
remDr <- rD$client
remDr$close()
rm(rD)
gc()
Slat answered 16/5, 2017 at 5:52 Comment(3)
hello. tried both of these ac couple of times, no go. the broswer closes, but when i try to restart the rD object, i always get error that the port 4567) is already in use. only quitting the R session altogether releases it.Whim
I have the same issue as @GuyManova.Lopes
So adding my 2 cents here. Seems like the solution doesn't work for the updated Rselenium package. Used the solution recommended by @Seb_ISU below , found on the thread here : github.com/ropensci/RSelenium/issues/228Studied
D
21

I did not have issues until recently. What worked for me is to use the solution above and as per the solution in this thread to add a line to kill the Java instance(s) inside RStudio.

remDr$close()
rD$server$stop()
rm(rD, remDr)
gc()

system("taskkill /im java.exe /f", intern=FALSE, ignore.stdout=FALSE)
Dominiquedominium answered 11/6, 2020 at 14:33 Comment(1)
Thank you soooo much. Been dealing with this annoying issues for months.Transposal
H
7

The command:

system("taskkill /im java.exe /f", intern=FALSE, ignore.stdout=FALSE)

will free all the ports.

If you want to free a particular port, you can do this:

#get the PID of the process you launched

pid <- driver$server$process$get_pid()

#pasting this PID in the following command (will kill all the child processes as well, closes the browser as well)

system(paste0("Taskkill /F /T" ," /PID ", pid))
Hallowell answered 1/8, 2020 at 23:24 Comment(2)
While I would love for a targeted kill, the above does not target the orphaned Java process---it did not work for me.Tumpline
Do you want to kill a specific child process?Hallowell
D
6

One way to avoid this problem is to use free_port() to find a free port (rather than specifying it manually)

library(netstat)
rsDriver(verbose = FALSE, port=free_port())
Deponent answered 15/11, 2020 at 14:57 Comment(0)
B
3

What worked for me is not calling stop at all and only calling close.

rD <- rsDriver(port = 4444L)
remDr <- rD[["client"]]
remDr$close()
rm(rD)
gc()

EDIT: Nevermind - this worked last week several times and then hasn't worked again.

Boyla answered 27/3, 2020 at 19:39 Comment(0)
S
0

I tried all the versions for a similar script:

   driver = rsDriver(browser = c("firefox"))
   remDr <- driver[["client"]]

... , which at some point started to give the next error:

    Error in wdman::selenium(port = port, verbose = verbose, version = version, 
: Selenium server signals port = 4567 is already in use.

Non of the suggestions worked for closing the port 4567, neither restarting of the browser and the RStudio, except for specifying a different port.

driver = rsDriver(browser = c("firefox"), verbose = FALSE, port = 4444L)
remDr <- driver[["client"]]

A list of ports (e.g. 4445L, 4446L, 4447L, etc) works as well or reloading the session by pressing Ctrl + Shift + F10

Scrambler answered 26/1, 2022 at 17:3 Comment(0)
S
0

Assuming that the issue arises after cleaning the environment in RStudio, I found the easiest solution was to simply garbage collect:

gc()
Swordsman answered 26/6, 2022 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.