We have a digital object repository called DSpace at work, and we use the SWORDv2 protocol to deposit digital object into it. DSpace GUI and it’s SWORDv2 endpoint runs as servlets in a Tomcat container, and it’s all behind Nginx acting as a reverse proxy.
The other day one of my co-workers wanted to deposit a larger digital object package ( 8 GB ) into the repository, but unfortunately it failed because the servlet kept throwing SocketTimeoutException while it was reading the data being deposited, so I had to investigate and solve the problem.
java.net.SocketTimeoutException: Read timed out
I read the Tomcat and DSpace logs but it revealed nothing. I noticed that DSpace had some interrupted deposits in it’s upload directory. All of the files were of size 2 GB, which was suspicious but I couldn’t figure out why at first, because I couldn’t see and find any limit that would explain why it should die at just 2 gigs.
I am not an Nginx expert, but I enabled debug logging and started reading logs. Unfortunately at first sight it didn’t reveal anything, I saw no errors, only that Tomcat returned 500 while depositing, that’s when the SocketTimeoutException was raised. However some lines caught my attention anyways.
2018/07/18 09:27:55 [debug] 4273#4273: *1 sendfile: @0 2147479552
2018/07/18 09:27:55 [debug] 4273#4273: *1 sendfile: 2147479552 of 2147479552 @0
That big integer was quite suspicous, and after doing some simple math I figured that 2147479552 twice divided by 1024 is 2048. Which means this could be a byte count. This made me start thinking. After sending this much data and some wait Tomcat sent 500 with that exception, so I figured it’s worth looking into. I started digging in Nginx’s source code and found a comment block and a constant below it:
* On Linux up to 2.4.21 sendfile() (syscall #187) works with 32-bit
* offsets only, and the including <sys/sendfile.h> breaks the compiling,
* if off_t is 64 bit wide. So we use own sendfile() definition, where offset
* parameter is int32_t, and use sendfile() for the file parts below 2G only,
* see src/os/unix/ngx_linux_config.h
* Linux 2.4.21 has the new sendfile64() syscall #239.
* On Linux up to 2.6.16 sendfile() does not allow to pass the count parameter
* more than 2G-1 bytes even on 64-bit platforms: it returns EINVAL,
* so we limit it to 2G-1 bytes.
#define NGX_SENDFILE_MAXSIZE 2147483647L
After some further digging I realized that this sendfile() call is the default network I/O implementation of Nginx, but it can be turned off by setting
in the http scope of the Nginx config file. As I suspected this solved the problem, and we could deposit the packages without problems. Now as a short summary here’s what this is about and what happened:
sendfile() is an I/O call that transfers data between file descriptors without having to first read the data into RAM, therefore it’s faster than the traditional solution of reading from the source, storing in RAM then writing to the destination. This is by default enabled in Nginx and this is what among other solutions makes Nginx a fast web server. However it has a limit of 2 GB. So when my co-worker was depositing his package, Nginx accepted the deposit, and sent it to Tomcat. The trouble was that it wouldn’t send all data. When it finished with the 2GB part of the 8 GB size file it just stopped, while Tomcat was still waiting for the rest of the data. After a short while it timed out, and returned an HTTP code of 500 to Nginx. Turning off sendfile() fixes this, as Nginx can now send all the data, however this makes network I/O slower.
In desktop applications we typically configure our application by reading a config file, and using the values found.
In a Java web application however there’s a better way, because there’s already a config file we can use, and this is the web.xml file, where we can place the so called context init parameters, that are loaded during the web application’s startup. In this article I’m going to show how this works.
There are 3 things we must do to make this all work
- Create a so called ContextListener subclass that will listen to events such as context initialization (servlet startup).
- Tell Tomcat we have such a listener class and that it should tell it about such events, by referencing our class in the web.xml file
- Place the parameters and values in the web.xml file
This is how a ContextListener class looks like:
public class ContextListener implements ServletContextListener
public void contextInitialized( ServletContextEvent event )
final ServletContext context = event.getServletContext();
final String dbdriver = context.getInitParameter( “dbdriver” );
final String dburl = context.getInitParameter( “dburl” );
public void contextDestroyed( ServletContextEvent event )
As we can see the class has 2 methods that need to be implemented, the contextInitialized and contextDestroyed events. We can read the context parameters in the contextInitialized class and then do whatever we want with them.
Let’s now see the relevant parts of such a web.xml file:
<?xml version=”1.0″ encoding=”UTF-8″?>
As we can see we have 2 parameters here dbdriver, and dburl, which are database connection configuration parameters in this case. Also we have the context event listener class references. If we restart Tomcat now when starting up the servlet it will tell the context listener about the startup and it can read the parameters.
That’s it. It’s this simple!
At work we still use Tomcat 7 in production and I needed to set up debugging for various development systems. This article shows how to enable Tomcat 7 remote debugging
Enabling Tomcat 7 remote debugging via JDWP
I use Ubuntu 16.04 LTE so I’ll use that in the example, but other distros will not be that much different, except for the path and (re)starting the service of course.
- Edit or create the file /usr/share/tomcat7/bin/setenv.sh and put in the following content:
export JAVA_OPTS=”-Xdebug \
Note: Obviously if the file already exists and it already has some content, then just add the parameters instead of adding the entire line.
- Restart Tomcat
sudo service tomcat7 restart
- Go to the Tomcat binary directory, which is by default
c:\Program Files\Apache Software Foundation\Tomcat 7.0\bin
- Start the program Tomcat7w.exe
- Switch to the java tab and add the following lines to the Java options textbox:
Note: It is important that each of the parameters should be added on separate lines, and that lines should have no whitespaces in the end!
- Restarts Tomcat 7
net stop tomcat7
net start tomcat7
Attaching Netbeans debugger to Tomcat 7
Now that we have Tomcat running with the remote debugging on we can attach Netbeans to debug.
- Click debug – attach debugger, a dialog box will appear
- Select Java Debugger (JPDA) as the Debugger
- Select SocketAttach as Connector
- Fill in host / IP address to the host field
- Fill in port to the Port field, in this example the port is 8787, but obviously it can be any non-taken port
- Click OK
- If everything went OK the debugging tab should show up showing the running threads
…and that’s it! Happy bug hunting!