Skip to content

Commit 4cf96ff

Browse files
authored
Merge pull request #18 from JasonAlt/master
Memory and hang issues found while testing
2 parents c0c109e + af615e5 commit 4cf96ff

3 files changed

Lines changed: 54 additions & 59 deletions

File tree

src/gridftp_hdfs.c

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ char gridftp_file_name[PATH_MAX];
3636
char gridftp_transfer_type[10];
3737

3838
static globus_mutex_t g_hdfs_mutex;
39-
static pthread_t g_thread_id;
40-
static int g_thread_pipe_fd;
39+
static pthread_t g_thread_id = 0;
40+
static int g_thread_pipe_fd = -1;
41+
static int g_thread_saved_stderr_fd = -1;
4142

4243
static globus_result_t check_connection_limits(const hdfs_handle_t *, int, int);
4344
static int dumb_sem_open(const char *fname, int flags, mode_t mode, int value);
@@ -54,15 +55,6 @@ void
5455
hdfs_destroy(
5556
void * user_arg);
5657

57-
void
58-
hdfs_start(
59-
globus_gfs_operation_t op,
60-
globus_gfs_session_info_t * session_info);
61-
62-
void
63-
hdfs_destroy(
64-
void * user_arg);
65-
6658

6759
/*
6860
* Interface definitions for HDFS
@@ -231,14 +223,22 @@ setup_hdfs_logging()
231223
globus_mutex_unlock(&g_hdfs_mutex);
232224
return;
233225
}
234-
if (-1 == dup3(pipe_fds[1], 2, O_CLOEXEC))
226+
227+
if ((g_thread_saved_stderr_fd = dup(STDERR_FILENO)) == -1)
228+
{
229+
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR, "Failed to save stderr for HDFS logging: (errno=%d, %s).\n", errno, strerror(errno));
230+
globus_mutex_unlock(&g_hdfs_mutex);
231+
return;
232+
}
233+
234+
if (-1 == dup3(pipe_fds[1], STDERR_FILENO, O_CLOEXEC))
235235
{
236236
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR, "Failed to reopen stderr for HDFS logging: (errno=%d, %s).\n", errno, strerror(errno));
237237
globus_mutex_unlock(&g_hdfs_mutex);
238238
return;
239239
}
240240
close(pipe_fds[1]);
241-
g_thread_pipe_fd = 2;
241+
g_thread_pipe_fd = STDERR_FILENO;
242242

243243
int *pipe_ptr = malloc(sizeof(int));
244244
if (pipe_ptr == NULL)
@@ -266,13 +266,6 @@ setup_hdfs_logging()
266266
int
267267
hdfs_activate(void)
268268
{
269-
if (globus_mutex_init(&g_hdfs_mutex, GLOBUS_NULL)) {
270-
globus_gfs_log_message(GLOBUS_GFS_LOG_ERR, "Unable to initialize global mutex");
271-
return 1;
272-
}
273-
g_thread_id = 0;
274-
g_thread_pipe_fd = -1;
275-
276269
globus_extension_registry_add(
277270
GLOBUS_GFS_DSI_REGISTRY,
278271
"hdfs",
@@ -289,23 +282,6 @@ hdfs_activate(void)
289282
int
290283
hdfs_deactivate(void)
291284
{
292-
if (g_thread_id > 0)
293-
{
294-
if (g_thread_pipe_fd >= 0)
295-
{
296-
fflush(stderr);
297-
close(g_thread_pipe_fd);
298-
}
299-
void *retval;
300-
pthread_join(g_thread_id, &retval);
301-
g_thread_id = 0;
302-
g_thread_pipe_fd = -1;
303-
}
304-
305-
globus_mutex_destroy(&g_hdfs_mutex);
306-
g_thread_id = 0;
307-
g_thread_pipe_fd = -1;
308-
309285
globus_extension_registry_remove(
310286
GLOBUS_GFS_DSI_REGISTRY, "hdfs");
311287

@@ -568,7 +544,7 @@ hdfs_start(
568544
globus_gfs_operation_t op,
569545
globus_gfs_session_info_t * session_info)
570546
{
571-
hdfs_handle_t* hdfs_handle;
547+
hdfs_handle_t* hdfs_handle = NULL;
572548
globus_gfs_finished_info_t finished_info;
573549
GlobusGFSName(hdfs_start);
574550
globus_result_t rc;
@@ -581,6 +557,12 @@ hdfs_start(
581557
int user_transfer_limit = -1;
582558
int transfer_limit = -1;
583559

560+
if (globus_mutex_init(&g_hdfs_mutex, GLOBUS_NULL)) {
561+
SystemError(hdfs_handle, "Unable to initialize mutex", rc);
562+
globus_gridftp_server_operation_finished(op, rc, &finished_info);
563+
return;
564+
}
565+
584566
hdfs_handle = (hdfs_handle_t *)globus_malloc(sizeof(hdfs_handle_t));
585567
memset(hdfs_handle, 0, sizeof(hdfs_handle_t));
586568

@@ -605,6 +587,7 @@ hdfs_start(
605587
globus_gridftp_server_operation_finished(op, rc, &finished_info);
606588
return;
607589
}
590+
608591
if (globus_mutex_init(hdfs_handle->mutex, GLOBUS_NULL)) {
609592
SystemError(hdfs_handle, "Unable to initialize mutex", rc);
610593
globus_gridftp_server_operation_finished(op, rc, &finished_info);
@@ -846,9 +829,32 @@ hdfs_destroy(
846829
globus_mutex_destroy(hdfs_handle->mutex);
847830
globus_free(hdfs_handle->mutex);
848831
}
832+
if (hdfs_handle->cvmfs_graft)
833+
free(hdfs_handle->cvmfs_graft);
849834
globus_free(hdfs_handle);
850-
free(hdfs_handle->cvmfs_graft);
851835
}
836+
837+
if (g_thread_id > 0)
838+
{
839+
if (g_thread_saved_stderr_fd >= 0)
840+
{
841+
dup2(g_thread_saved_stderr_fd, STDERR_FILENO);
842+
close(g_thread_saved_stderr_fd);
843+
}
844+
845+
if (g_thread_pipe_fd >= 0)
846+
{
847+
close(g_thread_pipe_fd);
848+
}
849+
850+
void *retval;
851+
pthread_join(g_thread_id, &retval);
852+
}
853+
854+
g_thread_id = 0;
855+
g_thread_pipe_fd = -1;
856+
g_thread_saved_stderr_fd = -1;
857+
852858
closelog();
853859
}
854860

@@ -915,18 +921,6 @@ set_close_done(
915921
if ((hdfs_handle->done_status == GLOBUS_SUCCESS) && (rc != GLOBUS_SUCCESS)) {
916922
hdfs_handle->done_status = rc;
917923
}
918-
if (g_thread_id > 0)
919-
{
920-
if (g_thread_pipe_fd >= 0)
921-
{
922-
fflush(stderr);
923-
close(g_thread_pipe_fd);
924-
}
925-
void *retval;
926-
pthread_join(g_thread_id, &retval);
927-
g_thread_id = 0;
928-
g_thread_pipe_fd = -1;
929-
}
930924
}
931925

932926
/*************************************************************************

src/gridftp_hdfs_cksm.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,15 @@ globus_result_t hdfs_save_checksum(hdfs_handle_t *hdfs_handle) {
394394
}
395395

396396
static globus_result_t
397-
hdfs_calculate_checksum(hdfs_handle_t *hdfs_handle, hdfsFS fs, const char *type)
397+
hdfs_calculate_checksum(hdfs_handle_t *hdfs_handle, const char *type)
398398
{
399399
globus_result_t rc = GLOBUS_SUCCESS;
400400

401401
GlobusGFSName(hdfs_calculate_checksum);
402402

403403
hdfs_initialize_checksums(hdfs_handle);
404404

405-
hdfsFile fd = hdfsOpenFile(fs, hdfs_handle->pathname, O_RDONLY, 0, 1, 0);
405+
hdfsFile fd = hdfsOpenFile(hdfs_handle->fs, hdfs_handle->pathname, O_RDONLY, 0, 1, 0);
406406
if (fd == NULL) {
407407
SystemError(hdfs_handle, "Failed to open file for checksumming", rc)
408408
return rc;
@@ -412,23 +412,23 @@ hdfs_calculate_checksum(hdfs_handle_t *hdfs_handle, hdfsFS fs, const char *type)
412412
void *buffer = malloc(cksum_buffer_size);
413413
if (buffer == NULL) {
414414
MemoryError(hdfs_handle, "Unable to allocate checksum temp buffer", rc);
415-
hdfsCloseFile(fs, fd);
415+
hdfsCloseFile(hdfs_handle->fs, fd);
416416
return rc;
417417
}
418418
ssize_t retval = 0;
419419
globus_off_t offset = 0;
420420
do {
421421
hdfs_update_checksums(hdfs_handle, buffer, retval);
422422
errno = 0; // older versions of libhdfs sometimes fails to reset errno.
423-
retval = hdfsRead(fs, fd, buffer, cksum_buffer_size);
423+
retval = hdfsRead(hdfs_handle->fs, fd, buffer, cksum_buffer_size);
424424
if ((retval == -1) && (errno == EINTR)) {continue;}
425425
offset += retval;
426426
} while (retval > 0);
427427
if (retval == -1) {
428428
SystemError(hdfs_handle, "Failed to read from file for checksumming", rc);
429429
// Fall-through
430430
}
431-
hdfsCloseFile(fs, fd);
431+
hdfsCloseFile(hdfs_handle->fs, fd);
432432

433433
if (rc == GLOBUS_SUCCESS) {
434434
hdfs_handle->offset = offset;
@@ -490,7 +490,7 @@ globus_result_t hdfs_get_checksum_internal(hdfs_handle_t *hdfs_handle, const cha
490490

491491
hdfsFile fh = hdfsOpenFile(fs, filename, O_RDONLY, 0, 0, 0);
492492
if (fh == NULL) {
493-
rc = hdfs_calculate_checksum(hdfs_handle, fs, requested_cksm);
493+
rc = hdfs_calculate_checksum(hdfs_handle, requested_cksm);
494494
if (rc != GLOBUS_SUCCESS) {return rc;}
495495
fh = hdfsOpenFile(fs, filename, O_RDONLY, 0, 0, 0);
496496
if (fh == NULL) {

src/gridftp_hdfs_error.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919

2020

2121
#define SystemError(hdfs_handle, msg, rc) \
22+
int system_errno = errno; \
2223
SomeError(hdfs_handle, msg) \
23-
rc = GlobusGFSErrorSystemError(formatted_msg, errno); \
24-
globus_free(formatted_msg);
24+
rc = GlobusGFSErrorSystemError(formatted_msg, system_errno); \
25+
globus_free(formatted_msg); \
2526

2627

2728
#define MemoryError(hdfs_handle, msg, rc) \

0 commit comments

Comments
 (0)